Killed app notification click intent putExtra method not working [closed]

青春壹個敷衍的年華 提交于 2020-03-23 23:56:57

问题


I have a problem intent.putExtra is not working when the app is killed. Wake up and running mode all works. I use firebase messaging service, getData() method, pending intent.

private PendingIntent getPendingIntent(int newsID) {
    Intent intent = new Intent(this, DetailedNewsActivity.class);
    intent.putExtra("newsID", newsID);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    // All the parents of SecondActivity will be added to task stack.
    stackBuilder.addNextIntentWithParentStack(intent);

    //PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingIntent;
}

回答1:


I guess your onMessageReceived() function is not getting triggered. There are two types of notifications from firebase data messages and other one is notification message. Currently you are getting notification message which doesn't invoke onMessageReceived() in killed state. Use "data" messages instead.This could be done by the back end developer.Please ask him to send data messages. Notification messages trigger onMessageRecieved() only when app is in foreground but data message invoke onMessageRecieved() in both foreground and background state

Please replace "notification" key from payload to "data" key. Please refer to this link Click for more information




回答2:


Check that data first then use that

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
   try {
      JSONObject json = new JSONObject("{\"data\":"+remoteMessage.getData().toString()+"}");
      //decode your json data and use that
    } catch (Exception e) {
       Log.e(TAG, "Exception: " + e.getMessage());
    }
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
    String stingMessage = remoteMessage.getNotification().getBody();
    //show the message string as notification or as your want
}

To show notification and add pending intent

Intent myintent = new Intent(this, YourActivityWantToL.class);
    myintent.putExtra("message", stingMessage);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(TAG)
                    .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg))
                    .setContentText(stingMessage);

    mBuilder.setContentIntent(contentIntent);

but if you are targeting and running app on 26 or higher you have to use notification channel



来源:https://stackoverflow.com/questions/59764322/killed-app-notification-click-intent-putextra-method-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!