我无法通知打开应用。我已按照Android文档上的说明操作,但无济于事。它创建通知没有问题,但点击它只是解雇它。
请帮忙!提前谢谢!
为什么点击不打开应用的通知?
Intent intent = new Intent(this, MainActivity.class);
String type = "";
if (extras.containsKey(KEY_TYPE)) type = extras.getString(KEY_TYPE);
String text = "";
if (type.equalsIgnoreCase(TYPE_MATCH_FOUND)) {
// TODO: send intent with all variables, trigger matched fragment when user goes into app
text = getResources().getString(R.string.msg_found_match);
intent.putExtra(KEY_TYPE, TYPE_MATCH_FOUND);
}
else if (type.equalsIgnoreCase(TYPE_MESSAGE)) {
// TODO: trigger chat fragment when user goes into app
text = getResources().getString(R.string.msg_new_message_from);
intent.putExtra(KEY_TYPE, TYPE_MESSAGE);
}
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("LFDate")
.setContentText(text)
.setAutoCancel(true)
.setLights(Color.parseColor("#0086dd"), 2000, 2000);
if (prefs.getNotificationVibrate()) mBuilder.setVibrate(new long[] {1000, 1000, 1000});
if (prefs.getNotificationSound()) mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
答案 0 :(得分:6)
我今天早些时候遇到了同样的问题,如果你使用kitkat,你将不得不改为:
// Have pending intent use FLAG_CANCEL_CURRENT, cause on
// kitkat you get a permission denied error
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
或者您可以将标记添加到接收方,或者以XML格式通知启动的活动:
android:exported="true"