Android Installation
In your AndroidManifest.xml
.....
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<application ....>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
.....
In android/settings.gradle
...
include ':react-native-push-notification'
project(':react-native-push-notification').projectDir = file('../node_modules/react-native-push-notification/RNPushNotificationAndroid')
In android/app/build.gradle
...
dependencies {
...
compile project(':react-native-push-notification')
}
Register module (in MainActivity.java
)
import android.content.Intent;
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
private ReactNativePushNotificationPackage mReactNativePushNotificationPackage;
@Override
protected String getMainComponentName() {
return "YOUR_APP_NAME";
}
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
mReactNativePushNotificationPackage = new ReactNativePushNotificationPackage(this);
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new VectorIconsPackage(),
new FabricPackage(),
mReactNativePushNotificationPackage
);
}
@Override
protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
mReactNativePushNotificationPackage.newIntent(intent);
}
....
}
Usage
var PushNotification = require('react-native-push-notification');
PushNotification.configure({
onRegister: function(token) {
console.log( 'TOKEN:', token );
},
onNotification: function(notification) {
console.log( 'NOTIFICATION:', notification );
},
senderID: "YOUR GCM SENDER ID",
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true,
});
Handling Notifications
When any notification is opened or received the callback onNotification
is called passing an object with the notification data.
Notification object example:
{
foreground: false,
message: 'My Notification Message',
data: {},
}
Local and Schedule Notifications
PushNotification.localNotification(details: Object)
PushNotification.localNotificationSchedule(details: Object)
(IOS ONLY)
EXAMPLE:
PushNotification.localNotification({
id: 0,
title: "My Notification Title",
ticker: "My Notification Ticker",
largeIcon: "ic_launcher",
smallIcon: "ic_notification",
message: "My Notification Message"
});
PushNotification.localNotificationSchedule({
message: "My Notification Message",
date: new Date()
});
Sending Notification Data From Server
Same parameters as PushNotification.localNotification()
iOS Only Methods
PushNotification.checkPermissions(callback: Function)
Check permissions
PushNotification.setApplicationIconBadgeNumber(number: number)
set badge number
PushNotification.getApplicationIconBadgeNumber(callback: Function)
get badge number
PushNotification.abandonPermissions()
Abandon permissions
TODO