2014-04-01 78 views
2

我想知道是否有標誌和參數可以告訴我用戶是否通過點擊通知欄中的推送通知來啓動活動/應用程序。如何通過點擊推送通知來啓動應用程序

我在C2DMReceiver.java

代碼
Context context = getApplicationContext(); 

     PackageManager manager = context.getPackageManager(); 
     Intent notificationIntent = manager 
       .getLaunchIntentForPackage(packageName); 
     notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
     **notificationIntent.putExtra("fromRemote", true);** 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 

     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(); 
     notification.icon = R.drawable.icon; 
     notification.tickerText = message; 
     notification.number = badge; 
     notification.when = System.currentTimeMillis(); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.defaults = Notification.DEFAULT_ALL; 
     notification.setLatestEventInfo(context, "Draw Something", message, 
       pendingIntent); 
     notification.contentIntent = pendingIntent; 
     notificationManager.notify(0, notification); 

我試着設置

notificationIntent.putExtra("fromRemote", true); 

,但應用程序推出的時候有在意向無需額外。

我在我的mainactivity

Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     print("onCreate - bundle has extras"); 
     for (String key: extras.keySet()) 
     { 
      Log.v ("mytag", "MainActivity onCreate key =" + key); 
     } 
    } 
    else { 
     Log.v ("mytag", "onCreate - bundle has NO extras"); 
    } 

輸出我得到的OnCreate函數代碼的onCreate - 束具有無需額外。所以臨時演員沒有得到通過。

那麼還有其他方法嗎?這很容易在iOS

+0

僅發佈一些東西到服務器時,你的'Receiver'得到所謂:) – Sadegh

+0

http://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity – Urban

+0

感謝Urban - 嘗試搜索各種格式的問題:) - 它的工作原理 – Anand

回答

1

只需發佈一個答案,以便訪問此頁面的人得到解決方案。

當您創建用於啓動應用程序的Intent時,需要使用putExtra(ID_KEY,id),在onCreate()方法中,您可以使用getIntent().getExtras().getInt(ID_KEY);來檢索傳遞的id整數。

傳遞的額外的可以是任何東西,一個布爾值,字符串等

來源 - https://stackoverflow.com/a/7358692/3036759

+0

除Firebase推送「通知」消息,直到通知已被點擊,應用程序纔會被調用。這意味着你不能設置任何ID。 – Baggers

相關問題