On clicking notification I am able to get call inside
PushNotification.popInitialNotification(notification) but the notification data is null, I need to route user to different screens based on the notification data.
Could any one please help.
Here is my function, the notification field is printed as null.
PushNotification.popInitialNotification(notification => this.handleNotification(notification));
handleNotification(notification) {
console.log("The notification is----->", notification);
}
@csooraj Hi! did you solve the issue ? my popInitialNotification function works only when application in foreground and in background but when i kill it i don't get notifications
Same problem here, on Android, popInitialNotification handler always returns null when app is in background or killed. Everything is working properly on iOS.
I checked out the installation steps a thousand times, I think my setup is fine...
(Using RN 0.45.1 and react-native-push-notification 3.0.1)
@csooraj did you manage to solve it ?
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 :
package com.dieam.reactnativepushnotification.modules;
import android.app.Activity;
import android.app.Application;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener {
public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag
private RNPushNotificationHelper mRNPushNotificationHelper;
private final Random mRandomNumberGenerator = new Random(System.currentTimeMillis());
private RNPushNotificationJsDelivery mJsDelivery;
private Bundle savedBundle = null;
public RNPushNotification(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this);
Application applicationContext = (Application) reactContext.getApplicationContext();
// The @ReactNative methods use this
mRNPushNotificationHelper = new RNPushNotificationHelper(applicationContext);
// This is used to delivery callbacks to JS
mJsDelivery = new RNPushNotificationJsDelivery(reactContext);
registerNotificationsRegistration();
}
@Override
public String getName() {
return "RNPushNotification";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
return constants;
}
public void onNewIntent(Intent intent) {
if(intent.hasExtra("google.message_id")){
Bundle bundle = intent.getExtras();
bundle.putBoolean("foreground", false);
intent.putExtra("notification", bundle);
this.savedBundle = bundle;
mJsDelivery.notifyNotification(bundle);
}
if (intent.hasExtra("notification")) {
Bundle bundle = intent.getBundleExtra("notification");
bundle.putBoolean("foreground", false);
intent.putExtra("notification", bundle);
mJsDelivery.notifyNotification(bundle);
}
}
private void registerNotificationsRegistration() {
IntentFilter intentFilter = new IntentFilter(getReactApplicationContext().getPackageName() + ".RNPushNotificationRegisteredToken");
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String token = intent.getStringExtra("token");
WritableMap params = Arguments.createMap();
params.putString("deviceToken", token);
mJsDelivery.sendEvent("remoteNotificationsRegistered", params);
}
}, intentFilter);
}
private void registerNotificationsReceiveNotificationActions(ReadableArray actions) {
IntentFilter intentFilter = new IntentFilter();
// Add filter for each actions.
for (int i = 0; i < actions.size(); i++) {
String action = actions.getString(i);
intentFilter.addAction(getReactApplicationContext().getPackageName() + "." + action);
}
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getBundleExtra("notification");
// Notify the action.
mJsDelivery.notifyNotificationAction(bundle);
// Dismiss the notification popup.
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int notificationID = Integer.parseInt(bundle.getString("id"));
manager.cancel(notificationID);
}
}, intentFilter);
}
@ReactMethod
public void requestPermissions(String senderID) {
ReactContext reactContext = getReactApplicationContext();
Intent GCMService = new Intent(reactContext, RNPushNotificationRegistrationService.class);
GCMService.putExtra("senderID", senderID);
reactContext.startService(GCMService);
}
@ReactMethod
public void presentLocalNotification(ReadableMap details) {
Bundle bundle = Arguments.toBundle(details);
// If notification ID is not provided by the user, generate one at random
if (bundle.getString("id") == null) {
bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
}
mRNPushNotificationHelper.sendToNotificationCentre(bundle);
}
@ReactMethod
public void scheduleLocalNotification(ReadableMap details) {
Bundle bundle = Arguments.toBundle(details);
// If notification ID is not provided by the user, generate one at random
if (bundle.getString("id") == null) {
bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt()));
}
mRNPushNotificationHelper.sendNotificationScheduled(bundle);
}
@ReactMethod
public void getInitialNotification(Promise promise) {
WritableMap params = Arguments.createMap();
Activity activity = getCurrentActivity();
if (activity != null) {
Intent intent = activity.getIntent();
Bundle bundle = null;
if (intent.hasExtra("notification")) {
bundle = intent.getBundleExtra("notification");
} else if (intent.hasExtra("google.message_id")) {
bundle = intent.getExtras();
}
if (this.savedBundle != null){
bundle = savedBundle;
this.savedBundle = null;
}
if (bundle != null) {
bundle.putBoolean("foreground", false);
String bundleString = mJsDelivery.convertJSON(bundle);
params.putString("dataJSON", bundleString);
}
}
promise.resolve(params);
}
@ReactMethod
public void setApplicationIconBadgeNumber(int number) {
ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(getReactApplicationContext(), number);
}
// removed @Override temporarily just to get it working on different versions of RN
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
onActivityResult(requestCode, resultCode, data);
}
// removed @Override temporarily just to get it working on different versions of RN
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Ignored, required to implement ActivityEventListener for RN 0.33
}
@ReactMethod
/**
* Cancels all scheduled local notifications, and removes all entries from the notification
* centre.
*
* We're attempting to keep feature parity with the RN iOS implementation in
* <a href="https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/RCTPushNotificationManager.m#L289">RCTPushNotificationManager</a>.
*
* @see <a href="https://facebook.github.io/react-native/docs/pushnotificationios.html">RN docs</a>
*/
public void cancelAllLocalNotifications() {
mRNPushNotificationHelper.cancelAllScheduledNotifications();
mRNPushNotificationHelper.clearNotifications();
}
@ReactMethod
/**
* Cancel scheduled notifications, and removes notifications from the notification centre.
*
* Note - as we are trying to achieve feature parity with iOS, this method cannot be used
* to remove specific alerts from the notification centre.
*
* @see <a href="https://facebook.github.io/react-native/docs/pushnotificationios.html">RN docs</a>
*/
public void cancelLocalNotifications(ReadableMap userInfo) {
mRNPushNotificationHelper.cancelScheduledNotification(userInfo);
}
@ReactMethod
public void registerNotificationActions(ReadableArray actions) {
registerNotificationsReceiveNotificationActions(actions);
}
}
Maybe I'll do a proper PR when I have time.
@ComicScrip can you point out what exactly you changed? Simple diff may not be enough if we're not using the same version of the library.
this is work thansk you
I changed the file as said, and still when I'm closing the app on the app list view with 'x'
The notification doesn't executed, In addition I don't see any error on the log.
I have checked and sure that my code gets there.
Is there a PR somewhere ? I can merge it as I think this bug affects a lot of people.
@ComicScrip Did you ever get a chance to get a PR up? If not do you mind if I jump in & submit one on your behalf? 馃槵
thank you! but I can't receive data message from response when foreground.
@ComicScrip What exactly did you change in that class?
I think this issue effects a lot of people, I have struggled with it for a few days and have had to delay releases. Would be good if we can get this PR merged in soon.
@ComicScrip Please make a PR, or @zo0r take fixes from ComicScrip and update module.
Solution doesn't work on 5.0
EDIT: Bug only occurs on huawei phones because we need to allow showing notifications on lock screen within phone settings.
It seems that https://github.com/zo0r/react-native-push-notification/pull/807 introduces some of the changes from @ComicScrip
I got this working by adding a message key to my FCM request, as described here:
https://github.com/zo0r/react-native-push-notification/blob/a071458feafc80a7a23cc43258d43f645833c0e9/trouble-shooting.md#4-mixed-remote-push-notifications
Adding the message key changed the notification from a silent one to a "mixed" one, which then started appearing on the phone even when the app was inactive or closed.
I also added the android:launchMode="singleTop" key to <activity> section in AndroidManifest.xml.
Issue still persist, anyone with a fix currently using "react-native-push-notification": "3.1.1"
@ComicScrip @clickclickonsal do you know if a PR was created for this and if we have a timeline?
@mazenchami I never heard back from @ComicScrip so I didn't created and I don't know if one was ever created for this. Unfortunately I'm looking at migrating off from using this project & using something else. :-/
@clickclickonsal what're you planning on migrating to? feel free to email me direct and move the thread elsewhere...
any updates? is this PR in v3.1.2?
This module is dead. RIP
@clickclickonsal what did you end up migrating to?
@mazenchami did you find a solution?
I did decide to continue using this library. The issue ended up being with the push provider I was using, Carnival. I decided to change our push provider & went to AWS SNS.
As far with the library, I did a complete uninstall of it & then removed everything related to it from the project(as I did not originally set it up on my project) with the app in a working state without it. So then I could proceed from scratch & followed the instructions in the ReadME.
Below is what My JS file looks like.
Since I'm using redux, I wrapped the PushNotification in a function so then I could pass it dispatch after redux is initialized in my app.
import { Platform, PushNotificationIOS, Alert } from 'react-native';
import PushNotification from 'react-native-push-notification';
import { handlePushNotification } from '../actions/notifications';
import { updatePushToken } from '../api';
import { store } from '../config/store';
export default dispatch =>
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function({ token }) {
const { session } = store.getState();
updatePushToken(
{
id: session.id,
pushToken: token,
},
session.token
);
},
// (required) Called when a remote or local notification is opened or received
onNotification: function(notification) {
if (Platform.OS === 'ios') {
// process the notification
// required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
notification.finish(PushNotificationIOS.FetchResult.NoData);
}
// process the notification
dispatch(handlePushNotification({ data: notification }));
alertMessage(notification);
},
// ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: 'INSER_YOUR_SENDER_ID_HERE',
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true,
});
const alertMessage = notification => {
Alert.alert(
'New Notification',
notification.message,
[
{
text: 'OK',
},
],
{ cancelable: true }
);
};
@Faolain I did find the solution after discussing with @clickclickonsal.
move the PushNotification.configure({...}); from the constructor to componentDidMount. worked for me
Thanks @dgurns, adding a message key to my FCM request work for me!
Hi,
Just to make precision on @mazenchami:
Put .configure() inside a component is a bad idea in most cases, component are not mounted/instanciate in background which can lead to more issues.
Hello I am getting notification=null in popInitialNotification. Please help me.
Most helpful comment
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.