2016-11-17 59 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");  
}