gpt4 book ai didi

android - 单击通知未打开应用程序/Activity

转载 作者:行者123 更新时间:2023-11-29 14:49:07 25 4
gpt4 key购买 nike

我在打开应用程序时遇到通知问题。我已按照 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());

最佳答案

我今天早些时候遇到了同样的问题,如果您使用的是 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 通知启动的 Activity :

 android:exported="true"

关于android - 单击通知未打开应用程序/Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24665483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com