2013-12-17 35 views
0

現在我創建一個通知意圖按照以下幾點:GCM消息發起一個Intent

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
Intent notificationIntent = new Intent(context, MyActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
notificationIntent.setAction(Long.toString(System.currentTimeMillis())); 

notificationIntent.putExtra("key", value); 

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
Notification updateComplete = new NotificationCompat.Builder(context) 
    .setContentTitle(title) 
    .setContentText(msg) 
    .setTicker(title) 
    .setWhen(System.currentTimeMillis()) 
    .setContentIntent(contentIntent) 
    .setDefaults(Notification.DEFAULT_SOUND) 
    .setAutoCancel(true) 
    .setSmallIcon(R.drawable.icon_notifications) 
    .build(); 

notificationManager.notify(100, updateComplete); 

當應用程序已經在後臺運行,一切正常。函數onNewIntent被調用,我從notificationIntent extras中獲取值。但是,如果應用程序不在後臺,它將轉到根用戶活動(登錄屏幕),該用戶將該用戶轉發到MyActivity。但根活動不會接到onNewIntent的呼叫,並且在用戶到達MyActivity時,附加信息將丟失。

反正有這個嗎?

我一直在嘗試在其他地方存儲值無濟於事......這包括共享偏好。

+0

kvish你可以檢查應用程序是否失控?如果應用程序正在運行,那麼不需要通知intent extra .right? – dipali

+0

我需要notificationIntent的原因是因爲我有一些數據需要傳遞給該Activity。否則,我怎麼知道用戶是否按下推送通知?我希望能夠從推送中提取一些數據並顯示相關信息...是否有更簡單的方法來執行此操作? – KVISH

回答

0

我發現瞭如何做到這一點。

基本上,如果您使用notificationIntent啓動的活動已經打開(如果它是當前用戶所在的活動),它將調用onNewIntent(Intent intent)。如果它當前不是活動活動或者應用程序未運行,它將啓動該活動並轉至onCreate()

您必須致電,致電getIntent()並檢查是否爲空。案件結案。

0

檢查this out。我不知道爲什麼你的活動應該運行以得到最新的結果。如果您按照該教程進行操作,那麼手機會自動獲得推送通知,如果有的話。無需在後臺運行活動。

+0

這不是我無法獲得通知。通知中有一些數據需要提取...這就是我面臨的問題。這就是爲什麼我需要'onNewIntent'函數叫做 – KVISH

1
@Override 
    protected void onMessage(final Context ctx, Intent intent) { 
if (CommonMethod.isAppicationRunning(ctx)) { 
      // Creates an explicit intent for an Activity in your app 
      Intent resultIntent = new Intent(ctx, ViewMessageDialog.class); 
      resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      resultIntent.putExtra("gcmmessage", message); 
      ctx.startActivity(resultIntent); 
     } else { 

      try { 
       sendNotification(message); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

/////////////////////// /////////////////////////////////

public static boolean isAppicationRunning(Context activity) { 
     ActivityManager am = (ActivityManager) activity 
       .getSystemService(EGLifeStyleApplication.mContext.ACTIVITY_SERVICE); 

     // get the info from the currently running task 
     List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
     ComponentName componentInfo = taskInfo.get(0).topActivity; 
     Log.i("current activity", "activity" 
       + activity.getClass().getSimpleName()); 

     if (componentInfo.getPackageName().equals(
       EGLifeStyleApplication.mContext.getPackageName())) { 
      return true; 
     } 
     return false; 
    } 

////////// ////////////////////////////////////////////////// ////////////////////////

private void sendNotification(String message) throws JSONException { 
     // this 
     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.app_icon; 
     CharSequence tickerText = message; // ticker-text 
     long when = System.currentTimeMillis(); 
     Context context = getApplicationContext(); 
     context.getClass().getSimpleName(); 
     Log.i("context.getClass().getSimpleName()", 
       "context.getClass().getSimpleName()=" 
         + context.getClass().getSimpleName()); 

     CharSequence contentTitle = "Product Received."; 
     CharSequence contentText = message; 
     Intent notificationIntent = null; 
     int notificationID = CommonVariable.notificationID; 

     notificationIntent = new Intent(this, ViewHomeScreen.class); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       notificationIntent, 0); 
     Notification notification = new Notification(icon, tickerText, when); 
     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_ALL; 
     notification.setLatestEventInfo(context, contentTitle, contentText, 
       contentIntent); 
     mNotificationManager.notify(notificationID, notification); 
    } 

檢查應用程序是否運行?如果應用程序運行在forgroung那麼請只打電話意圖通過該具體activity.and如果應用程序正在backgroung中運行,然後調用sendnotification()發送notifica通知欄上。

+0

我的問題更多地是圍繞這個:'notificationIntent.putExtra(「key」,value);',當通過單擊推送通知打開應用程序時,我需要該值。當應用程序已經打開(或在後臺)它可以工作,但是當應用程序沒有運行時,該值會丟失...我在通知欄中看到通知,我只是沒有看到來自'意外的'額外'。 – KVISH

+0

你可以使用廣播接收器,那麼你的價值不會丟失 – dipali

1

//這段代碼放在你gcmservice

Intent intent2 = new Intent(); 
       intent2.setAction("RECEIVE_MESSAGE_ACTION_NEW"); 
       intent2.putExtra("senderNum", senderNum); 
       intent2.putExtra("verificationCode", 
         ReturnValidationcode(message)); 
       context.sendBroadcast(intent2); 
+0

不知道如果我得到這個......閱讀'sendBroadcast',但是這是爲了我寫上述的notificationIntent? – KVISH

+0

http://avilyne.com/?p=267 – KVISH

+0

建立在你所做的,謝謝! – KVISH