-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PushNotification.popInitialNotification returns null #547
Comments
@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... @csooraj did you manage to solve it ? |
Finally solved the sh*t out of this ! 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' |
…(Android) * Stolen from zo0r#547
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. |
As reflected in zo0r#547 - ComicScrip's suggestion
@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. |
As reflected in zo0r#547 - ComicScrip's suggestion
@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 #807 introduces some of the changes from @ComicScrip |
I got this working by adding a Adding the I also added the |
Issue still persist, anyone with a fix currently using |
@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 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 |
Thanks @dgurns, adding a message key to my FCM request work for me! |
Hi, |
Hello I am getting notification=null in popInitialNotification. Please help me. |
I was having my Custom SplashActivity.java which was why it was not working on android //SpashActivity. |
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);
}
The text was updated successfully, but these errors were encountered: