1

I've followed this thread https://stackoverflow.com/a/38149137/6795758 to handle click_action and it works perfectly when app is closed. But when app is opened and if I push notification, app crashes showing the below error:

Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexo, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo}
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Bundle.unparcel()' on a null object reference
                                                                                   at android.os.Bundle.putAll(Bundle.java:182)
                                                                                   at android.content.Intent.putExtras(Intent.java:7098)
                                                                                   at com.seven.sample.fcm.ClickActionHelper.startActivity(ClickActionHelper.java:20)
                                                                                   at com.seven.sample.fcm.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:33)
                                                                                   at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
                                                                                   at com.google.firebase.iid.zzc.run(Unknown Source)
                                                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                   at java.lang.Thread.run(Thread.java:818)

Below are my codes of ClickActionHelper class and FirebaseMessaging service

ClickActionHelper.java

public class ClickActionHelper {

public static void startActivity(String className, Bundle extras, Context context){
    Class cls;

    try {
            cls = Class.forName(className);
            Intent i = new Intent(context, cls);
            i.putExtras(extras);
            context.startActivity(i);


    }catch(ClassNotFoundException e){
        Log.e("NotificationError",e.getMessage());
        //means you made a wrong input in firebase console
    }

 }
}      

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyGcmListenerService";

@Override
public void onMessageReceived(RemoteMessage message) {

    Map<String, String> data = message.getData();
    if (data.containsKey("click_action")) {
        ClickActionHelper.startActivity(data.get("click_action"), null, this);
    }

    String image = message.getNotification().getIcon();
    String title = message.getNotification().getTitle();
    String text = message.getNotification().getBody();
    String sound = message.getNotification().getSound();

    int id = 0;
    Object obj = message.getData().get("id");
    if (obj != null) {
        id = Integer.valueOf(obj.toString());
    }

    this.sendNotification(new NotificationData(image, id, title, text, sound));
}


/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param notificationData GCM message received.
 */
private void sendNotification(NotificationData notificationData) {

    Intent intent = new Intent(this, LandingPage.class);
    intent.putExtra(notificationData.getTitle(), notificationData.getTextMessage());

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = null;
    try {

        notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Sample")
                .setContentText(notificationData.getTextMessage()+"")
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pendingIntent);

    } catch (Exception e) {
        e.printStackTrace();
    }

    if (notificationBuilder != null) {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationData.getId(), notificationBuilder.build());
    } else {
        Log.d(TAG, "NotificationBuilder");
    }
}

}

AL.
  • 36,815
  • 10
  • 142
  • 281
top secret
  • 45
  • 2
  • 9
  • You must be creating the new NotidicationData object with some null parameters. Put some logs inside onMessageReceived method to see if every variable (image, id, title, text, sound) are initialized and not null. – Bloodeye Sep 13 '18 at 08:53
  • You're passing a `null` Bundle to your ClickActionHelper then trying to put it in your extras. This might be causing the NPE. Try adding a null checker before adding the bundle and see if you could still encounter the issue. – AL. Sep 13 '18 at 08:55
  • thanks @AL that helped – top secret Sep 13 '18 at 14:13
  • You're welcome. Did that solve the issue? I could put in an answer so that we could tag your post as answered. Cheers! – AL. Sep 13 '18 at 14:59
  • 1
    @AL yes it did..thanks for the clarification – top secret Sep 14 '18 at 03:24

1 Answers1

0

In your onMessageReceived(), you're calling startActivity():

ClickActionHelper.startActivity(data.get("click_action"), null, this);

passing a null bundle then inserting it in your intent extras:

i.putExtras(extras);

which is causing the NPE.

AL.
  • 36,815
  • 10
  • 142
  • 281