在带有多个通知的launchMode =“ singleTop”的活动中从NotificationIntent中获得更多收益

Jaydipsinh Zala

我同时与多发通知问题putExtra使用PendingIntentActivity getStringExtrafomIntentputExtra生成时最后返回Notification

首先让我解释完整的情况,如果我错了,请让我正确。

首先,我Activity的(就是它的MainActivitylaunchModesingleTop我将其设置为manifest.ie

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    ...    />

现在,我正在使用此代码生成通知

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notifyIntent = new Intent(context,
            MainActivity.class);
    notifyIntent.putExtra("message", msg);
    Log.i(TAG, msg);
    notifyIntent.putExtra("title", notification_title);
    Log.i(TAG, notification_title);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            BeaconService.this, 0, notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(
            context).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(notification_title).setContentText(msg)
            .setAutoCancel(true).setContentIntent(pendingIntent).build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notificationManager.notify(NOTIFICATION_ID, notification);
    NOTIFICATION_ID++;

我也在使用flag PendingIntent.FLAG_UPDATE_CURRENT但是,while clicking on any Notification (let's say we've 5 notifactions) it just returns the fifth extra put while generating Notification all top four notification extra are just like lost somewhere

在我中,MainActivity我也onNewIntent

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // getIntent() should always return the most recent
        setIntent(intent);
        if (intent.hasExtra("message")) {
            String message = intent.getStringExtra("message");
        }
        if (intent.hasExtra("title")) {
            String title = intent.getStringExtra("title");
        }
    }

返回的这些额外费用始终来自上次通知的额外费用。我不知道我要去哪里错了?

我也尝试了一些链接,但没有找到有用的解决方案。

使用PendingIntent for GCM发送时,Intent Extras丢失了

请帮忙。

大卫·水

您不是在创建唯一PendingIntent的。在Stackoverflow上,对于这个问题必须有一千个答案。

当您致电:

PendingIntent pendingIntent = PendingIntent.getActivity(
        BeaconService.this, 0, notifyIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);

第二次,它将找到第一个,PendingIntent并向您返回对其的引用(Android不会创建另一个)。此外,由于您进行了设置FLAG_UPDATE_CURRENT,因此Android会Intent通过用Intent新的中提供的附加功能覆盖旧的附加功能来修改基础Intent这就是为什么它总是只交付一个Intent,并且Intent总是拥有最后一组附加功能的原因。

您需要使每个PendingIntent唯一。有许多方法可以做到这一点(设置其他功能不是其中一种方法)。尝试提供一个唯一的数字作为的requestCode参数PendingIntent.getActivity()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

设置launchMode =“ singleTask”与设置活动launchMode =“ singleTop”

来自分类Dev

带有Nativescript中的按钮的持久通知

来自分类Dev

R中带有NA的累积收益

来自分类Dev

带有多个按钮的通知

来自分类Dev

FactoryGirl中带有块的多个参数

来自分类Dev

Odoo - 在 attrs 中带有 OR 的多个条件

来自分类Dev

Laravel获得带有多个标签的帖子

来自分类Dev

iOS中带有“解析”的新消息通知

来自分类Dev

iOS 7中带有iCloud通知的核心数据

来自分类Dev

在Tizen中播放带有通知的音频文件

来自分类Dev

带有警报管理器的android中的每日通知

来自分类Dev

iOS中带有“解析”的新消息通知

来自分类Dev

Swift中带有核心数据的KVO通知

来自分类Dev

接收有关Android中多个特征的通知

来自分类Dev

折叠的通知中带有通知内容扩展的小缩略图

来自分类Dev

具有singleInstance launchMode和新线程的活动

来自分类Dev

Android通知启动新的活动实例,无论将launchMode和/或标志设置为

来自分类Dev

如何处理JavaScript中的多个收益

来自分类Dev

资源字典中带有标题的多个项目组合框?

来自分类Dev

带有多个参数的Knockout JS中的扩展程序

来自分类Dev

带有多个串行器导轨的串行器中的阵列

来自分类Dev

subprocess.Popen中的多个变量,带有%dict()

来自分类Dev

Django-urls.py中带有DetailViews的多个<pk>

来自分类Dev

带有Bootstrap 3.0的导航栏中的多个折叠按钮

来自分类Dev

R中带有图例的多个彩色方框图

来自分类Dev

Lucene中带有多个单词的通配符查询

来自分类Dev

.split()中带有if语句的多个可能选项

来自分类Dev

matplotlib中的多个图,带有返回图的函数

来自分类Dev

在多个cpp文件中包含带有定义的h文件

Related 相关文章

热门标签

归档