IBM Cloud Push Notifications service React Native SDK
npm install bmd-push-react-native
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
- Automatic installation
- Manual installation
- Dependencies
- iOS
- Android FCM
- Set up SDKs
- iOS
- Android
- Initialize SDK
- Register for notifications
- UnRegister from push
- Push Notification service tags
- Retrieve tags
- Subscribe to a tag
- Retrieve subscriptions
- Unsubscribing from tag
- 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 ,
``JS`
$ react-native install bmd-push-react-native
You can link the package like this,
``JS``
$ react-native link bmd-push-react-native
If you want to link it manually ,
- iOS
1. In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name]node_modules
2. Go to ➜ bmd-push-react-native and add RNBmdPushReact.xcodeprojlibRNBmdPushReact.a
3. In XCode, in the project navigator, select your project. Add to your project's Build Phases ➜ Link Binary With Libraries
- Android
1. Open up android/app/src/main/java/[...]/MainActivity.javaimport com.bmdpush.react.RNBmdPushReactPackage;
- Add to the imports at the top of the filenew RNBmdPushReactPackage()
- Add to the list returned by the getPackages() methodandroid/settings.gradle
2. Append the following lines to :``
include ':bmd-push-react-native'
project(':bmd-push-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/bmd-push-react-native/android')android/app/build.gradle
3. Insert the following lines inside the dependencies block in :``
compile project(':bmd-push-react-native')
#### CocoaPods
1. Open the ios directory and add use_frameworks! and Swift version in the Podfile.`Swift`
use_frameworks!
ENV['SWIFT_VERSION'] = '5.0'
OR
`Swift
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
`pod install
2. run and open your in Xcode.
3. You need to add an empty Swift file in the app to build it successfully. This is for the swift bridging header.
!image
Create a firebase project and add the following bundle ids for android,
1. Add your bundle Id and com.ibm.mobilefirstplatform.clientsdk.android.push . Download the google-services.json and add inside the android ➜ app.
2. In the root build.gradle ➜ buildscript add the following ,
`XML`
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.0.1'
}
3. Go to android ➜ app ➜ build.gradle and add the following after dependencies {....},
`XML`
apply plugin: 'com.google.gms.google-services'
Set up SDKs
Open the iOS app in XCode and do the following ,
1. Under the Capabilities section enable the Push Notifications Background modes
2. Enable the for Remote notifications and Background fetchBuild Settings
3. Go to and make the following changes
a. 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 .
1. Add xmlns:tools="http://schemas.android.com/tools" in the tag
For example
`XML`
2. Add the following permissions,
`XML`
3. Add tools:replace="android:allowBackup" inside the tag
For example
`XML`
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:launchMode="singleTask"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
4. Add the following inside the ,
`XML`
5. Add the following lines,
`XML
`
Now you can build and run the android app from android studio or using the react-native run-android command.
Import the following dependecnice ,
`JS`
import {Push} from 'bmd-push-react-native';
import { DeviceEventEmitter } from 'react-native';
To initialize Push use the following code,
- Initialize without additional options
`JS
// 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
`JS
// 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
`JS
// 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
`JS
// 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);
});
`
`JS`
push.unregisterDevice().then(function(response) {
alert("Success unregisterDevice : " + response);
}).catch(function(e) {
alert("UnRegister error : " + e);
});
`JS`
Push.retrieveAvailableTags().then(function(response) {
alert("get tags : " + response);
}).catch(function(e) {
alert("get tags error : " + e);
});
`JS`
Push.subscribe(response[0]).then(function(response) {
alert("subscribe tags : " + response);
}).catch(function(e) {
alert("subscribe tags error : " + e);
});
`JS
Push.retrieveSubscriptions().then(function(response) {
alert("retrieveSubscriptions tags : " + response);
}).catch(function(e){
alert("error retrieveSubscriptions : " + e);
});
`
`JS``
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
* Visit the IBM Cloud Developers Community.
* Getting started with IBM MobileFirst Platform for iOS
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.