【问题标题】:Killed app notification click intent putExtra method not working [closed]杀死应用程序通知点击意图 putExtra 方法不起作用[关闭]
【发布时间】:2020-01-16 06:47:57
【问题描述】:

我有一个问题intent.putExtra 在应用程序被终止时无法正常工作。唤醒和运行模式一切正常。我使用 firebase 消息服务,getData() 方法,待定意图。

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

【问题讨论】:

  • 你能详细说明你的问题吗?
  • 我使用 FirebaseMessagingService, messageEvent.getData() 方法发送通知。允许所有通知,设置 onclick 挂起意图以启动活动。
  • 您的活动是在待定意图上打开的启动器活动吗?
  • 不是子活动
  • 您必须将意图传递给启动器或启动器活动,然后您需要导航。因为当应用程序运行时它工作正常,但在终止应用程序后,待定意图将进入启动器活动。试一次。

标签: android firebase android-intent push-notification


【解决方案1】:

我猜你的 onMessageReceived() 函数没有被触发。 firebase 数据消息有两种类型的通知,另一种是通知消息。目前,您收到的通知消息不会在终止状态下调用 onMessageReceived()。改用“数据”消息。这可以由后端开发人员完成。请让他发送数据消息。 通知消息仅在应用程序处于前台时触发 onMessageRecieved(),但数据消息在前台和后台状态均调用 onMessageRecieved()

请将有效载荷中的“通知”键替换为“数据”键。 请参考此链接Click了解更多信息

【讨论】:

  • 我使用数据消息
【解决方案2】:

先检查该数据,然后使用该数据

// 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
}

显示通知并添加待处理的意图

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

但如果您的目标是在 26 或更高版本上运行应用程序,则必须使用通知渠道

【讨论】:

  • 所有通知都是允许的,这个数据方法没有问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多