IBM Cloud Mobile Services - Client SDK React Native for Push Notifications service
The IBM Cloud Push Notifications service provides a unified push service to send real-time notifications to mobile and web applications. The SDK enables react-native apps to receive push notifications sent from the service.
Ensure that you go through IBM Cloud Push Notifications service documentation before you start.
- Version History
- Prerequisites
- Installation
- Dependencies
- Set up SDKs
- Initialize SDK
- Register for notifications
- UnRegister from push
- Push Notification service tags
- Samples and videos
- 1.3.0 - Added support for CocoaPods and tokyo region
- 1.2.0 - Added Android title
- Xcode 10+
- Android: minSdkVersion 16+, compileSdkVersion 28+
- React Native >= 0.57.8
- React Native CLI >= 2.0.1
Install the bmd-push-react-native
using ,
$ react-native install bmd-push-react-native
You can link the package like this,
$ react-native link bmd-push-react-native
If you want to link it manually ,
- iOS
- In XCode, in the project navigator, right click
Libraries
âžśAdd Files to [your project's name]
- Go to
node_modules
âžśbmd-push-react-native
and addRNBmdPushReact.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNBmdPushReact.a
to your project'sBuild Phases
âžśLink Binary With Libraries
- Android
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.bmdpush.react.RNBmdPushReactPackage;
to the imports at the top of the file - Add
new RNBmdPushReactPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:
include ':bmd-push-react-native'
project(':bmd-push-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/bmd-push-react-native/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:
compile project(':bmd-push-react-native')
- Open the
ios
directory and adduse_frameworks!
andSwift version
in thePodfile
.
use_frameworks!
ENV['SWIFT_VERSION'] = '5.0'
OR
use_frameworks!
target 'Your Target Name' do
pod 'RNBmdPushReact', :path => '../node_modules/bmd-push-react-native'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if ['BMSPush', 'BMSCore', 'BMSAnalyticsAPI'].include? target.name
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '5.0'
end
end
end
end
- run
pod install
and open your<your-app>.xcworkspace
in Xcode. - You need to add an empty Swift file in the app to build it successfully. This is for the swift bridging header.
Create a firebase project and add the following bundle ids for android,
-
Add
your bundle Id
andcom.ibm.mobilefirstplatform.clientsdk.android.push
. Download thegoogle-services.json
and add inside theandroid
âžśapp
. -
In the root
build.gradle
âžśbuildscript
add the following ,
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.0.1'
}
-
Go to
android
âžśapp
âžśbuild.gradle
and add the following afterdependencies {....}
,apply plugin: 'com.google.gms.google-services'
Open the iOS app in XCode and do the following ,
-
Under the Capabilities section enable the
Push Notifications
-
Enable the
Background modes
forRemote notifications
andBackground fetch
-
Go to
Build Settings
and make the following changesa. locate
Other Linker Flags
and add-lc++
,-ObjC
and$(inherited)
Now you can build and run the iOS app from Xcode or using the react-native run-ios
command.
Add the following inside the AndroidManifest.xml
file .
- Add
xmlns:tools="http://schemas.android.com/tools"
in the<manifest ...>
tag
For example
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.pushsample">
- Add the following permissions,
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- Add
tools:replace="android:allowBackup"
inside the<application ..>
tag
For example
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:launchMode="singleTask"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
- Add the following inside the
<activity android:name=".MainActivity" ....>
,
<intent-filter>
<action android:name="yourapp.bundle.IBMPushNotification" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
- Add the following lines,
<activity android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushNotificationHandler" android:theme="@android:style/Theme.NoDisplay"/>
<service android:exported="true" android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushIntentService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:exported="true" android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Now you can build and run the android app from android studio or using the react-native run-android
command.
Import the following dependecnice ,
import {Push} from 'bmd-push-react-native';
import { DeviceEventEmitter } from 'react-native';
To initialize Push use the following code,
- Initialize without additional options
// Initialize for push notifications without passing options
Push.init({
"appGUID":"xxxxxx-xxxx-41xxxx67-xxxxx-xxxxx",
"clientSecret":"xxxxx-xxxxx-xxxx-xxxxx-xxxxxxx",
"region":"us-south"
}).then(function(response) {
alert("InitSuccess: " + response);
}).catch(function(e) {
alert("Init Error: " + e);
});
- Initialize with additional options
// Initialize for push notifications by passing options
// Initialize for iOS actionable push notifications, custom deviceId and varibales for Parameterize Push Notifications
var optionsJson = {
"categories": {
"Category_Name1":[{
"IdentifierName":"IdentifierName_1",
"actionName":"actionName_1",
"IconName":"IconName_1"
},{
"IdentifierName":"IdentifierName_2",
"actionName":"actionName_2",
"IconName":"IconName_2"
}
]},
"deviceId":"mydeviceId",
"variables":{"username":"ananth","accountNumber":"536475869765475869"}
};
Push.init({
"appGUID":"xxxxxx-xxxx-41xxxx67-xxxxx-xxxxx",
"clientSecret":"xxxxx-xxxxx-xxxx-xxxxx-xxxxxxx",
"region":"us-south",
"options": optionsJson
}).then(function(response) {
alert("Init Success: " + response);
}).catch(function(e) {
alert("Init Error: " + e);
});
**IMPORTANT: These are the following supported regions
- "us-south", "eu-gb" , "au-syd", "eu-de", "us-east", and "jp-tok"
The following options are supported:
-
Register without UserId
// Register device for push notification without UserId var options = {}; Push.register(options).then(function(response) { alert("Success: " + response); }).catch(function(e) { alert("Register Error: " + e); });
-
Register with UserId
// Register device for push notification with UserId var options = {"userId":"ananthreact"}; Push.register(options).then(function(response) { alert("Success: " + response); }).catch(function(e) { alert("Register Error: " + e); });
push.unregisterDevice().then(function(response) {
alert("Success unregisterDevice : " + response);
}).catch(function(e) {
alert("UnRegister error : " + e);
});
Push.retrieveAvailableTags().then(function(response) {
alert("get tags : " + response);
}).catch(function(e) {
alert("get tags error : " + e);
});
Push.subscribe(response[0]).then(function(response) {
alert("subscribe tags : " + response);
}).catch(function(e) {
alert("subscribe tags error : " + e);
});
Push.retrieveSubscriptions().then(function(response) {
alert("retrieveSubscriptions tags : " + response);
}).catch(function(e){
alert("error retrieveSubscriptions : " + e);
});
var tag = "tag1";
Push.unsubscribe(tag).then(function(response) {
alert("unsubscribe tag : " + response);
}).catch(function(e) {
alert("Error : " + e);
});
-
For samples, visit - Github Sample
-
For video tutorials visit - IBM Cloud Push Notifications
Twitter | YouTube | Blog | Facebook |
======================= Copyright 2020-21 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.