如何从通知点击发送参数到活动?

Vid*_*nes 193 notifications android bundle android-intent

我可以找到一种方法从我的通知中向我的活动发送参数.

我有一个创建通知的服务.当用户点击通知时,我想用一些特殊参数打开我的主要活动.例如项目ID,因此我的活动可以加载并呈现特殊项目详细信息视图.更具体地说,我正在下载一个文件,当下载文件时,我希望通知有一个意图,当点击它时,它会以特殊模式打开我的活动.我试图使用putExtra我的意图,但似乎无法提取它,所以我认为我做错了.

我的服务中创建通知的代码:

        // construct the Notification object.
     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());


    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.icon);
    contentView.setTextViewText(R.id.text, tickerText);
    contentView.setProgressBar(R.id.progress,100,0, false);
    notif.contentView = contentView;        

    Intent notificationIntent = new Intent(context, Main.class);
    notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notif.contentIntent = contentIntent;

    nm.notify(id, notif);
Run Code Online (Sandbox Code Playgroud)

我的Activity中的代码尝试从通知中获取额外参数:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Log.i( "dd","Extra:" + extras.getString("item_id") );
    }
Run Code Online (Sandbox Code Playgroud)

额外的东西总是空的,我从来没有在我的日志中得到任何东西.

顺便说一句... onCreate只有在我的活动开始时才会运行,如果我的活动已经开始,我也想收集额外费用并根据我收到的item_id展示我的活动.

有任何想法吗?

Luc*_* S. 234

查看本指南(创建通知)并对ApiDemos"StatusBarNotifications"和"NotificationDisplay"进行采样.

要管理活动是否已经运行,您有两种方法:

  1. 在启动活动时向Intent 添加 FLAG_ACTIVITY_SINGLE_TOP标志,然后在activity类中实现 onNewIntent(Intent intent)事件处理程序,这样就可以访问为活动调用的新intent(这与调用getIntent不同) (),这将始终返回启动您的活动的第一个Intent.

  2. 与第一个相同,但不是在Intent中添加标志,而是必须在AndroidManifest.xml活动中添加"singleTop".

如果您使用intent extras,请记住PendingIntent.getActivity()使用该标志进行调用PendingIntent.FLAG_UPDATE_CURRENT,否则将为每个通知重复使用相同的额外内容.

  • 并回答用户关于extras为null的问题:你必须使用标志`PendingIntent.FLAG_UPDATE_CURRENT`调用`PendingIntent.getActivity()`,否则将为每个通知重用相同的额外内容. (91认同)
  • 如果您应该有不同的通知,除了在`PentIntent`上的`Intent`和`FLAG_UPDATE_CURRENT`上设置`FLAG_ACTIVITY_SINGLE_TOP`之外,还要确保在调用`PendingIntent.getActivity()`时使用不同的请求ID.请参阅http://stackoverflow.com/questions/7370324/notification-passes-old-intent-extras (7认同)
  • 你节省了我的一天,但为什么在android中传输这样复杂的简单数据 (2认同)

Muh*_*ria 93

我有类似的问题,我的应用程序显示消息通知.当有多个通知并单击每个通知时,它会在视图消息活动中显示该通知详细信息.我解决了在视图消息意图中收到相同额外参数的问题.

这是解决这个问题的代码.用于创建通知Intent的代码.

 Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
    notificationIntent.putExtra("NotificationMessage", notificationMessage);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);
Run Code Online (Sandbox Code Playgroud)

用于查看消息活动的代码.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    if(extras != null){
        if(extras.containsKey("NotificationMessage"))
        {
            setContentView(R.layout.viewmain);
            // extract the extra-data in the Notification
            String msg = extras.getString("NotificationMessage");
            txtView = (TextView) findViewById(R.id.txtMessage);
            txtView.setText(msg);
        }
    }


}
Run Code Online (Sandbox Code Playgroud)


小智 28

也许有点晚了,但是:而不是:

public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    Log.i( "dbg","onNewIntent");

    if(extras != null){
        Log.i( "dbg", "Extra6 bool: "+ extras.containsKey("net.dbg.android.fjol"));
        Log.i( "dbg", "Extra6 val : "+ extras.getString("net.dbg.android.fjol"));

    }
    mTabsController.setActiveTab(TabsController.TAB_DOWNLOADS);
}
Run Code Online (Sandbox Code Playgroud)

用这个:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("keyName");
}
Run Code Online (Sandbox Code Playgroud)

  • 对于OP来说可能已经晚了,但对于互联网上的其他人来说,它永远不会迟到:) (19认同)

小智 18

在这里遇到同样的问题.我通过使用不同的请求代码解决它,使用相同的ID作为通知,同时创建PendingIntent.但仍然不知道为什么要这样做.

PendingIntent contentIntent = PendingIntent.getActivity(context, **id**, notificationIntent, 0);
notif.contentIntent = contentIntent;
nm.notify(**id**, notif);
Run Code Online (Sandbox Code Playgroud)


Vid*_*nes 13

在阅读了一些电子邮件列表和其他论坛之后,我发现这个技巧似乎为意图添加了独特的数据.

像这样:

   Intent notificationIntent = new Intent(Main.this, Main.class);
   notificationIntent.putExtra("sport_id", "sport"+id);
   notificationIntent.putExtra("game_url", "gameURL"+id);

   notificationIntent.setData((Uri.parse("foobar://"+SystemClock.elapsedRealtime()))); 
Run Code Online (Sandbox Code Playgroud)

我不明白为什么需要这样做,它与意图有关,只能通过它的附加物来识别......

  • Anroid重用意图,意图动作和请求代码使其独特,但不是额外的数据.因此,您需要设置唯一的请求ID或使用不同的intent操作. (7认同)

Dhe*_*han 9

我尝试了一切但没有任何效果.

最终提出了以下解决方案.

1- in manifest为活动添加android:launchMode ="singleTop"

2-在进行挂起意图时执行以下操作,使用bundle而不是直接使用intent.putString()或intent.putInt()

                    Intent notificationIntent = new Intent(getApplicationContext(), CourseActivity.class);

                    Bundle bundle = new Bundle();
                    bundle.putString(Constants.EXAM_ID,String.valueOf(lectureDownloadStatus.getExamId()));
                    bundle.putInt(Constants.COURSE_ID,(int)lectureDownloadStatus.getCourseId());
                    bundle.putString(Constants.IMAGE_URL,lectureDownloadStatus.getImageUrl());

                    notificationIntent.putExtras(bundle);

                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
                            new Random().nextInt(), notificationIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT); 
Run Code Online (Sandbox Code Playgroud)

  • 这个对我有用.必须使用不同的requestCode值和PendingIntent.FLAG_UPDATE_CURRENT标志. (2认同)

小智 5

AndroidManifest.xml

包括 launchMode="singleTop"

<activity android:name=".MessagesDetailsActivity"
        android:launchMode="singleTop"
        android:excludeFromRecents="true"
        />
Run Code Online (Sandbox Code Playgroud)

短信接收器.java

为 Intent 和 PendingIntent 设置标志

Intent intent = new Intent(context, MessagesDetailsActivity.class);
    intent.putExtra("smsMsg", smsObject.getMsg());
    intent.putExtra("smsAddress", smsObject.getAddress());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(context, notification_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

消息详细信息Activity.java

onResume() - 每次都被调用,加载额外内容。

Intent intent = getIntent();
    String extraAddress = intent.getStringExtra("smsAddress");
    String extraBody = intent.getStringExtra("smsMsg");
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助,它基于 stackoverflow 上的其他答案,但这是对我有用的最新版本。