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来获得不同的通知状态栏上,使用时区唯一标识符