【问题标题】:GCM notification executes only onceGCM 通知只执行一次
【发布时间】:2013-04-06 02:50:28
【问题描述】:

我成功地从我的 GCM 服务器接收到我的 Android 应用的通知。

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

以下是我的 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 活动是当前视图?

【问题讨论】:

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


    【解决方案1】:

    来自文档

    public void notify (int id, Notification notification)

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

    您应该始终生成一个唯一 id 而不是 0,以便在状态栏上获得不同的通知,使用时区作为唯一标识符

    【讨论】:

      【解决方案2】:

      我浏览了你的代码,它对我来说非常完美,我真的很想知道它有什么问题,直到我发现你的 IntenetSerivce 没有它应该做的 constructor,它会像以下:

      public GCMIntentService(){
              super(PROJECT_ID);
          } 
      

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

      【讨论】:

      • 感谢您的 cmets。我试过你的方法,但似乎 super 不接受参数 - 它只是给出了一个错误
      • PROJECT_ID 必须是 String PROJECT_ID="number_of_your_project_you_ got_from_google_API_console",并且 IntetntServices 必须具有至少具有 IntentService 本身名称的Consutrocter
      • 再次感谢。我尝试了您的方法,但没有任何区别。我用一些新的观察更新了我原来的问题
      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多