使用Phonegap PushPlugin在后台和Push Notification到达时无法打开App

时间:2013-09-25 18:21:06

标签: android cordova push-notification phonegap-plugins

我正在使用Phonegap插件“PushPlugin”(https://github.com/phonegap-build/PushPlugin)以及适用于iOS和Android的Phonegap 2.9.0。对于iOS,一切都按预期工作:通知到达,我点击通知,应用程序启动。

在Android上,我们区分了两种情况:应用程序处于前台(活动)或后台(关闭或仅未使用)。当我在前台收到通知时,该插件可以正常工作。当我在后台收到通知时,插件会在通知栏中创建通知,但点击通知不会打开我的应用。

应该打开我的应用的相关代码是:

//  Gets called when the notification arrives
protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);

    // Extract the payload from the message
    final Bundle extras = intent.getExtras();
    if (extras != null)
    {
        final boolean   foreground = this.isInForeground();

        extras.putBoolean("foreground", foreground);

        if (foreground)
            PushPlugin.sendExtras(extras);
        else
            createNotification(context, extras);
    }
}

    // This creates the notification in the notification bar, but a click on the notification does not open my app
public void createNotification(Context context, Bundle extras)
{
    final NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    final String appName = getAppName(this);

    final Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
    .setSmallIcon(context.getApplicationInfo().icon)
    .setWhen(System.currentTimeMillis())
    .setContentTitle(appName)
    .setTicker(appName)
    .setContentIntent(contentIntent);

    final String message = extras.getString("message");
    if (message != null) {
        mBuilder.setContentText(message);
    } else {
        mBuilder.setContentText("<missing message content>");
    }

    final String msgcnt = extras.getString("msgcnt");
    if (msgcnt != null) {
        mBuilder.setNumber(Integer.parseInt(msgcnt));
    }

    mNotificationManager.notify(appName, NOTIFICATION_ID, mBuilder.build());
    tryPlayRingtone();
}

1 个答案:

答案 0 :(得分:1)

我认为这是一个与https://github.com/phonegap-build/PushPlugin/issues/35#issuecomment-24722796类似的问题,但这只是Android Manifest的一个问题:PushPlugin活动是用错误的包输入的。