簡體   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