【问题标题】:GCM Cordova - Tap not opening appGCM Cordova - 点击不打开应用程序
【发布时间】:2013-05-28 23:49:44
【问题描述】:

我正在使用 GCM Cordova(昨天使用 Cordova 2.6 下载)我发现虽然我已经成功接收到通知,但使用示例代码。但是,点击通知只会将其清除并关闭通知列表 - 不会启动应用程序本身。

在连接到 Eclipse 的手机上运行应用程序显示 GCMIntentService 接收到消息 (onMessage) 并最终运行“释放唤醒锁”,但应用程序没有打开。不应该,还是我必须更改示例中的代码才能实现这一点。

我还注意到,如果我强制停止应用程序,将不再收到通知。这正常吗?

【问题讨论】:

    标签: cordova google-cloud-messaging


    【解决方案1】:

    似乎答案并不简单——而是涉及到建立一个意图。

    我按照这篇文章开始: http://www.adobe.com/devnet/phonegap/articles/android-push-notifications-with-phonegap.html

    此时水龙头不起作用,所以我做了以下操作。在 Manifest 中添加了以下代码:

    <intent-filter>
    <action android:name="NAMEOFAPP.STARTME" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    

    此代码被放置在 GCMIntentService.java 文件中 - 它替换了上面项目引用的大部分通知代码:

        String message = extras.getString("message");
        String title = extras.getString("title");
    
        Intent nintent = new Intent("uk.ac.chester.uocmobile.STARTME");
        nintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    
        nintent.addCategory(Intent.CATEGORY_DEFAULT);
        PendingIntent pintent = PendingIntent.getActivity(context, 0, nintent, 0);
    
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setContentIntent(pintent)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setAutoCancel(true)
                    .setContentText(message)
                    .setDefaults(Notification.DEFAULT_ALL);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(0, mBuilder.build());
    

    您需要将此导入添加到同一个文件中:

                import android.support.v4.app.NotificationCompat;
    

    如果您在没有以下过程的情况下执行此操作,则会在编译时出错。

    右键单击您的项目,然后转到 Android 工具 -> 添加支持库。添加 Android 支持库,修订版 13。这会将这些调用添加到您的项目中并使其全部正常工作。

    当您在此之后点击通知时,它将打开您的应用 - 将您发送到您设置的主页。这可能并不理想,但对我有用。我添加了 onstart 和 onresume 以确保 iOS 或 Android 上的应用程序(HTML/JS 格式)会以与 FaceBook 应用程序相同的方式检查新消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2014-12-09
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多