我如何通知通知?

时间:2016-11-17 14:29:19

标签: android android-intent notifications

为什么Intent来自通知无效?

Intent resultIntent = new Intent(context, myclass.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Bundle extras=new Bundle();
        extras.putString("key","value");
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
            NotificationCompat.Builder n = new NotificationCompat.Builder(context)
                    .setContentTitle("test")
                    .setContentText(text)
                    .setSound(sound)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .setExtras(extras)
                    .setSmallIcon(android.R.drawable.ic_media_play);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService((context.getApplicationContext().NOTIFICATION_SERVICE));
            notificationManager.notify(0, n.build());
    }

但是在myclass的onStart()中,当我调用getintent().getExtras()时,结果是 null ,为什么? 我怎样才能getExtras()通知Intent?

4 个答案:

答案 0 :(得分:2)

我想你忘记打电话了。

resultIntent.putExtras(extras);

在打电话之前。

答案 1 :(得分:2)

这对我有用,你可以从中得到想法:

 Intent notificationClickIntent = new Intent(context, NotificationAppListing.class);
            notificationClickIntent.putExtra("notificationType", notificationTypeOne);
            notificationClickIntent.putExtra("notificationUrl", notificationUrl);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(Search.class);
        stackBuilder.addNextIntent(notificationClickIntent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(mId, notificationBuilder.build());

答案 2 :(得分:1)

你应该将你的额外内容添加到resultIntent,而不是NotificationCompat.Builder(而不是resultPendingIntent

Bundle extras=new Bundle();
extras.putString("key","value");
resultIntent.putExtras(bundle);

或简单地说:

resultIntent.putExtra("key","value");

答案 3 :(得分:1)

你需要放置

resultIntent.putExtras(bundle)

在捆绑后分配键值对
那么你可以得到值

Bundle extras = getIntent().getExtras();
if (extras != null) {
String yourValue = extras.getString("key");    
}