繁体   English   中英

在后台并且推送通知到达时,使用Phonegap PushPlugin无法打开应用程序

[英]Using Phonegap PushPlugin does not open App when in background and Push Notification arrives

我正在使用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();
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM