使用PhoneGap的PushPlugin不会在后台时打开App和推送通知到达 [英] Using Phonegap PushPlugin does not open App when in background and Push Notification arrives

查看:254
本文介绍了使用PhoneGap的PushPlugin不会在后台时打开App和推送通知到达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap的插件PushPlugin( https://github.com/phonegap-build / PushPlugin )一起的PhoneGap 2.9.0适用于iOS和Android。对于iOS一切正常:一个通知到达时,我点击通知和应用程序启动

在Android上,我们区分两种情况:该应用程序在前台(活动),或在后台(关闭或只是没有主动使用)。当我收到通知,而在前台,插件工作。当我收到通知,而在后台,插件会在通知栏通知,但窃听的通知不打开我的应用程序。

相关code应该打开我的程序是:

  //当通知到达被调用
保护无效的onMessage(上下文的背景下,意图意图){
    Log.d(TAG的onMessage - 背景:+背景);    //从消息中提取有效载荷
    最终捆绑额外= intent.getExtras();
    如果(临时演员!= NULL)
    {
        最终布尔前景= this.isInForeground();        extras.putBoolean(前景,前景);        如果(前景)
            PushPlugin.sendExtras(临时演员);
        其他
            createNotification(背景下,临时演员);
    }
}    //这会在通知栏通知,但通知的点击并不会打开我的应用程序
公共无效createNotification(上下文的背景下,捆绑演员)
{
    最后NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    最后弦乐的appName = getAppName(本);    最终意图notificationIntent =新意图(这一点,PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra(pushBundle,临时演员);    最终的PendingIntent contentIntent = PendingIntent.getActivity(这一点,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);    最后NotificationCompat.Builder mBuilder =
            新NotificationCompat.Builder(上下文)
    .setSmallIcon(context.getApplicationInfo()图标)
    .setWhen(System.currentTimeMillis的())
    .setContentTitle(的appName)
    .setTicker(的appName)
    .setContentIntent(co​​ntentIntent);    最终字符串消息= extras.getString(信息);
    如果(消息!= NULL){
        mBuilder.setContentText(消息);
    }其他{
        mBuilder.setContentText(<丢失邮件内容和GT;);
    }    最后弦乐msgcnt = extras.getString(msgcnt);
    如果(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清单的问题:PushPlugin活性进入一个错误的包。

I'm using the Phonegap plugin "PushPlugin" (https://github.com/phonegap-build/PushPlugin) together with Phonegap 2.9.0 for iOS and Android. For iOS everything works as expected: A notification arrives, I click on the notification and the app is started.

On Android, we distinguish between two cases: The app is in foreground (active) or in background (closed or just not actively used). When I receive a notification while in foreground, the plugin works. When I receive a notification while in background, the plugin creates a notification in the notification bar, but tapping on the notification does not open my app.

The relevant code that should open my app is:

//  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();
}

解决方案

I thought it was a problem similar to https://github.com/phonegap-build/PushPlugin/issues/35#issuecomment-24722796, but it was simply an issue with the Android Manifest: The PushPlugin activity was entered with a wrong package.

这篇关于使用PhoneGap的PushPlugin不会在后台时打开App和推送通知到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆