0

我成功地從我的GCM服務器接收到我的Android應用程序的通知。GCM通知只執行一次

當我第一次選擇我的通知時,我的通知會啓動目標活動。 但是,以下通知不會啓動該活動。

以下是我的GCMIntentService類:

public class GCMIntentService extends GCMBaseIntentService{ 
    private String TAG = "RT-GCM"; 

    @Override 
    protected void onError(Context arg0, String arg1) { 
     // TODO Auto-generated method stub 
     Log.e(TAG, "GCM-SERVICE: error"); 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     int nid = Integer.parseInt(intent.getStringExtra("nid")); 

     try { 
//   generateNotification(context, nid, intent.getStringExtra("message")); 
      generateNotification(context, intent); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onRegistered(Context context, String arg1) { 
     // TODO Auto-generated method stub 
     Log.e(TAG, "GCM-SERVICE: Registered"); 

     AccountManager accountManager = AccountManager.get(context); 
     accountManager.getAccounts(); 
     Account account = getAccount(accountManager); 

     new GCMServerCall().register(arg1, "Tony", account.name); 
    } 

    @Override 
    protected void onUnregistered(Context arg0, String regID) { 
     // TODO Auto-generated method stub 
     Log.e(TAG, "GCM-SERVICE: UN-Registered"); 
     new GCMServerCall().unregister(regID); 
    } 

    /** 
    * Issues a notification to inform the user that server has sent a message. 
    * 
    * I copied this code fragment from a GCM.zip example file downloaded from http://cl.ly/0C151616032t 
    */ 
    private static void generateNotification(Context context, Intent myintent) { 

     Integer nid = Integer.parseInt(myintent.getStringExtra("nid")); 

     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, myintent.getStringExtra("message") + "["+nid+"]", when); 
     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, ArticleReader.class); 
     Log.e("TESTING ...", "Sending NID#" + nid); 
     notificationIntent.putExtra("NID", nid); 
     notificationIntent.putExtra("nid", nid); 
     notificationIntent.putExtra("test", "working"); 

     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, myintent.getStringExtra("message") + "["+nid+"]", intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
     notification.defaults = Notification.DEFAULT_ALL; 
     notificationManager.notify(0, notification); 
    } 

    /**** 
    * retrieving Google account(s) registered on the target device 
    * @param accountManager 
    * @return 
    */ 
    private Account getAccount(AccountManager accountManager){ 
     Account[] accounts = accountManager.getAccountsByType("com.google"); 
     Log.i(TAG, "We have " + accounts.length + " accounts!"); 
     return accounts[0]; 
    } 

} 

UPDATE:

我已經發現了一些新的東西。在第一次也是唯一一次通過選擇通知成功啓動ArticleReader.class之後,選擇以下通知僅適用於如果將應用程序從ArticleReader移開的情況。換言之,如果ArticleReader的實例是活動視圖,則任何試圖啓動相同活動的通知都將不起作用。

任何人都可以告訴我如何通過通知重複啓動ActivityReader類,即使ActivityReader活動是當前視圖嗎?

回答

0

我經過你的代碼,它是如此完美的我,我真的不知道什麼是錯的,直到我發現你IntenetSerivce沒有constructor這是它應該做,這將是類似如下:

public GCMIntentService(){ 
     super(PROJECT_ID); 
    } 

其中PROJECT_ID是你的項目,你從谷歌API控制檯獲取的ID。

+0

謝謝您的意見。我試過你的方法,但它似乎超級沒有參數 - 它只是給了一個錯誤 – sisko 2013-04-06 05:14:51

+0

PROJECT_ID必須是String PROJECT_ID =「number_of_your_project_you_ got_from_google_API_console」,並且IntetntServices必須有Consutrocter至少有IntentService本身的名稱 – 2013-04-06 05:27:02

+0

再次感謝。我嘗試了你的方法,但沒有任何區別。我用一些新的觀察結果更新了我的原始問題 – sisko 2013-04-06 10:58:00

1

從文檔

public void notify (int id, Notification notification)

發佈一個通知在狀態欄顯示。 id - 此通知的標識符在您的應用程序中是唯一的。

而不是0,你應該總是生成一個唯一的ID來獲得不同的通知狀態欄上,使用時區唯一標識符