1
我正在使用Phonegap插件「PushPlugin」(https://github.com/phonegap-build/PushPlugin)和Phonegap 2.9.0(適用於iOS和Android)。對於iOS,一切都按預期工作:通知到達後,我點擊通知並啓動應用程序。使用PhoneGap PushPlugin在後臺和推送通知到達時不會打開App
在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();
}