Skip to content
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

Closed
csooraj opened this issue Sep 5, 2017 · 28 comments
Closed

PushNotification.popInitialNotification returns null #547

csooraj opened this issue Sep 5, 2017 · 28 comments

Comments

@csooraj
Copy link

csooraj commented Sep 5, 2017

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 csooraj closed this as completed Sep 6, 2017
@Clcll
Copy link

Clcll commented Oct 19, 2017

@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

@ComicScrip
Copy link

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 ?

@ComicScrip
Copy link

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.

@noambonnie
Copy link

@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.

@aspidvip
Copy link

this is work thansk you

@oron11
Copy link

oron11 commented Jun 1, 2018

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.

@Gp2mv3
Copy link
Contributor

Gp2mv3 commented Jul 12, 2018

Is there a PR somewhere ? I can merge it as I think this bug affects a lot of people.

@clickclickonsal
Copy link

@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? 😬

@voxuanthuan
Copy link

thank you! but I can't receive data message from response when foreground.

akbokha added a commit to datacamp/react-native-push-notification that referenced this issue Jul 16, 2018
As reflected in zo0r#547 - ComicScrip's suggestion
@Luckygirlllll
Copy link

@ComicScrip What exactly did you change in that class?

@veermangat
Copy link

veermangat commented Jul 23, 2018

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.

steelbrain pushed a commit to Truebill/react-native-push-notification that referenced this issue Jul 31, 2018
As reflected in zo0r#547 - ComicScrip's suggestion
@lorenc-tomasz
Copy link
Contributor

@ComicScrip Please make a PR, or @zo0r take fixes from ComicScrip and update module.

@lorenc-tomasz
Copy link
Contributor

lorenc-tomasz commented Aug 29, 2018

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.

@lorenc-tomasz
Copy link
Contributor

lorenc-tomasz commented Aug 29, 2018

It seems that #807 introduces some of the changes from @ComicScrip

@dgurns
Copy link

dgurns commented Sep 18, 2018

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.

@linux08
Copy link

linux08 commented Oct 5, 2018

Issue still persist, anyone with a fix currently using "react-native-push-notification": "3.1.1"

@mazenchami
Copy link

@ComicScrip @clickclickonsal do you know if a PR was created for this and if we have a timeline?

@clickclickonsal
Copy link

@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. :-/

@mazenchami
Copy link

@clickclickonsal what're you planning on migrating to? feel free to email me direct and move the thread elsewhere...

@verybluebot
Copy link

any updates? is this PR in v3.1.2?

@1nfinity-5starZ
Copy link

This module is dead. RIP

@Faolain
Copy link

Faolain commented Mar 23, 2019

@clickclickonsal what did you end up migrating to?

@mazenchami did you find a solution?

@clickclickonsal
Copy link

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 }
  );
};

@mazenchami
Copy link

@Faolain I did find the solution after discussing with @clickclickonsal.

move the PushNotification.configure({...}); from the constructor to componentDidMount. worked for me

@CarrilloJuan
Copy link

Thanks @dgurns, adding a message key to my FCM request work for me!

@Dallas62
Copy link
Collaborator

Dallas62 commented Aug 4, 2020

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.

@Dallas62 Dallas62 closed this as completed Aug 4, 2020
@haresh4d
Copy link

haresh4d commented Aug 9, 2020

Hello I am getting notification=null in popInitialNotification. Please help me.

@Hassan-Riaz
Copy link

I was having my Custom SplashActivity.java which was why it was not working on android

//SpashActivity.
Intent intent = new Intent( getApplicationContext() ,MainActivity.class);
Bundle extras = getIntent().getExtras();
//just pass the extras to main activity then you are good to go.
if (extras != null) {
intent.putExtras(extras);
}
this.startActivity(intent);
finish();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests