【问题标题】:Send Notification ID through to activity for each notfication将通知 ID 发送到每个通知的活动
【发布时间】:2015-10-12 10:17:59
【问题描述】:

我现在很困惑,我已经阅读了几个 SE 示例,它们似乎都在做一些稍微不同的事情。

我想做的是:有一个名为NotificationActivity 的活动,当我单击通知时,它必须打开该活动并为该活动提供DeviceId。我不想覆盖或更新任何待处理的活动。每个活动都应该有自己的意图。

NotificationActivity 的实例应该只有一次。

这是我目前的代码:

MyGcmListenerService:

Intent intent = new Intent(this, NotificationActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    //ADD My Extras
    intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_ID, content.DeviceId);
    intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_TYPE_ID, content.DeviceTypeId);
    intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_NAME, content.DeviceName);
    //
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);   

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(content.DeviceName)
            .setContentText("Notification")
            .setAutoCancel(true)
            .setSound(ingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setGroup("Mi:" + content.DeviceId)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NotificationID++, notificationBuilder.build());

还有我的 NotificationActivity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    deviceId = getIntent().getExtras().getLong(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_ID, -1);
    deviceName = getIntent().getExtras().getString(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_NAME, "");
    deviceTypeId = getIntent().getExtras().getInt(CommonBundleAttributes.CONNECTING_ACTIVITY_DEVICE_TYPE_ID, 0);

我在这里缺少什么/我认为我与所有不同的标志和启动器类型混淆了。

如果内存中已经有一个NotificationActivty,我想关闭它并用最新的意图打开一个新的。如果用户手机上有 3 个通知,并且他们点击了所有三个。它必须为最后一次点击的通知打开 NotificationActivty。

我的待处理意图一定有问题?

【问题讨论】:

    标签: android android-intent android-notifications


    【解决方案1】:

    在 NotificationActivity 中,您可以使用以下代码收集 int 或 long 值,即

    notificationID = getIntent().getExtras().getInt(CommonBundleAttributes.CONNECTING_ACTIVITY_NOTIFICATIONID, 0);
    

    然后请使用 putExtra() 传递一个适当的值,即如果收集 int 然后传递 Integer.parseInt(content.DeviceId) 或收集 long 然后传递 Long.parseLong(content.DeviceId) 希望对你有帮助

    【讨论】:

      【解决方案2】:

      在通知类中添加这一行

      intent.putExtra(CommonBundleAttributes.CONNECTING_ACTIVITY_NOTIFICATIONID, NotificationID);
      

      在通知活动中

      notificationID = getIntent().getExtras().getInt(CommonBundleAttributes.CONNECTING_ACTIVITY_NOTIFICATIONID, 0);
      

      【讨论】:

      • 这与我所做的有什么不同?您将Long 更改为Int?我的deviceId 很长?
      • 你根据你的要求改变你的数据类型......你不是意图通知ID......
      • 那么你要改变什么?你是在暗示我在做什么?
      • 嗨纳伦德拉。我注意到您正在为很多问题添加投票请求。请不要这样做 - 我们更喜欢人们自然地获得选票,而不是破坏帖子来获得选票。如果他们可能不知道点赞和接受是如何工作的,可以提醒 cmet 中的人,但不要过度!
      猜你喜欢
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2023-04-06
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      相关资源
      最近更新 更多