android - 使用 Phonegap PushPlugin 在后台和推送通知到达时不会打开应用程序

标签 android cordova push-notification phonegap-plugins

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

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

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

//  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 Activity 输入了错误的包。

关于android - 使用 Phonegap PushPlugin 在后台和推送通知到达时不会打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19012354/

相关文章:

android - 输入时清除 EditText 中的文本

java - Android Google Map V3 PolyLine无法绘制

android - 谷歌地图中的 fatal error

android - Phonegap Build ios 6 iphone 5 启动画面不会消失

android - IndexedDB 在应用程序安装后自动恢复

android-接收推送通知文本并在 Activity 中显示

android - 如何解决 "This campaign targets 0 app user(s) (0% of potential users)."(Firebase) (Android)

ios - 切换到企业分发后推送通知不起作用

android - iOS中有没有类似于Android的dimens.xml的东西?

ios - 无法使用 Azure DevOps 构建使用 Cordova/Ionic 构建的 iOS 应用程序