GCM通知仅执行一次

时间:2013-04-06 02:50:29

标签: android notifications android-activity launch google-cloud-messaging

我已成功接收来自GCM服务器的通知到我的Android应用程序。

当我第一次选择通知时,我的通知会启动目标活动。 但是,以下通知不会启动活动。

以下是我的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];
    }

}

更新

我发现了一些新东西。在第一次也是唯一一次通过选择通知成功启动ArticleReader.class之后,只有在我远离ArticleReader导航应用程序时,才会选择以下通知。换句话说,如果ArticleReader的实例是活动视图,则尝试启动相同活动的任何通知都将无效。

有人可以告诉我如何通过通知重复启动ActivityReader类,即使ActivityReader活动是当前视图吗?

2 个答案:

答案 0 :(得分:1)

来自docs

public void notify (int id, Notification notification)

  

发布要在状态栏中显示的通知。    id - 此通知的标识符在您的应用程序中是唯一的。

您应该始终生成一个唯一的ID来获取状态栏上的不同通知,而不是0,您应该使用时区来获取唯一标识符

答案 1 :(得分:0)

我查看了你的代码,它对我来说非常完美,我真的想知道它有什么问题,直到我发现你的IntenetSerivce没有它应该做的constructor,它会如下所示:

public GCMIntentService(){
        super(PROJECT_ID);
    } 

其中PROJECT_ID是您从Google API控制台获得的项目ID。