我使用PhoneGap的插件“PushPlugin”( https://github.com/phonegap-build/PushPlugin )与PhoneGap的2.9.0适用于iOS和Android一起。 对于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();
}