2016-02-06 104 views
3

我通過BroadcastReceiver在我的應用程序中得到通知,問題是,我如何解析數據到一個活動並與接收到的數據進行對話?android-如何顯示一個對話框,當點擊通知

這是我的代碼,但是當我點擊該通知,沒有任何反應:

NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(Intent.ACTION_VIEW); 
     notificationIntent.setData(Uri.parse(link)); 
     PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

     Notification myNotification = new NotificationCompat.Builder(ctx) 
       .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setLargeIcon(remote_picture) 
       .setContentTitle(onvan).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
       .setContentText(msg).setContentIntent(pending).build(); 
     notificationManager.notify(1, myNotification); 

我有一些像variablese鏈接,味精,onvan包含我的數據,我需要這些變量發送到活動並進行對話。

我該怎麼做?

+0

就地檢查我的答案。 – Harshad

+0

http://stackoverflow.com/a/9751134/3514144 –

+0

使用活動作爲對話http://stackoverflow.com/a/1979631/1168654 –

回答

0

試試這個:

Intent notifyIntent = new Intent(context,YourActivityClassHere.class); 
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK); 
//UNIQUE_ID if you expect more than one notification to appear 
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID, 
      notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

只是使的PendingIntent打開你的活動之一,讓你的活動是完全透明的,只是打開一個對話框。

編輯:如果你想打開從通知單擊事件的活動:

假設通知符是你通知對象:

Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0); 
notif.contentIntent = contentIntent; 

因爲在這裏更詳細的訪問。 Android - Prompt Dialog window when touch on Notification

1

可以使用方法putExtra從您的通知發送數據到另一個Activity,並把這個

PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)); 

PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);` 
相關問題