2016-11-17 31 views
0

为什么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()结果是,为什么呢? 如何才能从getExtras()通知Intent?

回答

2

我想你忘了打电话。

resultIntent.putExtras(extras); 

在调用它之前。

+0

确定,但又是什么在NotificationCompat.Builder .setExtras(演员),以及如何获得它? – spyker10

+0

实际上我从来没有用过..但我发送数据的通知就像我说的方式..是否工作? –

1

你应该添加额外给你resultIntent,不NotificationCompat.Builder(而不是resultPendingIntent

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

或者干脆:

resultIntent.putExtra("key","value"); 
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()); 
1

您需要放置

resultIntent.putExtras(bundle) 

包分配后立即用键值对
那么你就可以得到价值

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