react-native-push-notification: Can't receive notifications when app is closed or in background
manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="co.my.packagename.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="co.my.packagename.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
in the application:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
`<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="co.my.packagename" />
</intent-filter>
</receiver>`
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</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>
settings.gradle:
include ':app' ... include ':react-native-push-notification' project(':react-native-push-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-notification/android')
build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:1.2.0'
}
`
app/build.gradle ` dependencies { compile fileTree(dir: “libs”, include: [“*.jar”]) compile “com.android.support:appcompat-v7:23.0.1” compile ‘com.android.support:support-v4:19.1.+’ … compile project(‘:react-native-push-notification’)
compile ('com.google.android.gms:play-services-gcm:8.1.0')
}
Mainapp.java
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
@Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), ... new ReactNativePushNotificationPackage() ); } };
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 34 (4 by maintainers)
My app receives GCM push notifications when it is in the background, tested on Android 4.1 and 5.1.
It doesn’t receive push notifications when its not running though. Although, I’m not sure that feature is supported yet.
bump
I had the same problem when I was testing PN. Logcat error:
Cannot send to notification centre because there is no 'message' field in: Bundle
Just added
message
field and it worked.Finally solved the sh*t out of this ! I know it’s probalbly very hacky but it works. Modify the content of node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java as follow :
Maybe I’ll do a proper PR when I have time.
This turned out to be very simple. PushNotification.configure goes into componentDidMount inside your index.js. This will generate a unique device token that you can copy from the console log and use in the server code to send notification to this specific device.
The trick is to add PushNotification.localNotification() inside onNotification, so that once a remote notif is received, it is converted into local one. Unfortunately, the documentation doesn’t make this part clear but a remote notif won’t go into the device notifs centre unless it’s converted to local.
I can see the notification when the app is closed but as soon as I click on it and open the app the
onNotification
doesn’t fire again. I have everything set like the instructions.@npomfret do you got it working?
Neither I get notifications when the app is closed. I get them when the app is in the foreground or in the background, but not when it is closed.
When the app is closed, adb logcat spits out:
11-02 20:05:10.578 11769 11769 W GCM-DMM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.CENSORED (has extras) }
Does it matter if I’m using local notification?
Done! Push Notifications are showing up, even when app is completely closed. Just had to update to the current version of this module, react and react-native and then import and place PushNotification.configure directly inside the index.android.js (even before the component class), similar to the link @oddball posted.