gpt4 book ai didi

android - 单击通知未启动由服务创建的我的 PendingIntent

转载 作者:行者123 更新时间:2023-11-29 14:31:50 27 4
gpt4 key购买 nike

所以这段代码是在我的服务开始在前台启动后从内部运行的。这也会创建将留在那里的通知。我有很多问题/疑问:

1) 点击通知只会打开应用信息,不会打开我的 PendingIntent。

2) 未设置我的内容标题和文本。我收到“MyApp 正在运行。触摸以获取更多信息或停止 Activity 。”显示在通知中而不是“TEST1 TEST2”。

3) note.flags|=Notification.FLAG_NO_CLEAR 似乎是多余的。我之前把它注释掉了,在我尝试清除所有通知后,通知仍然留在那里。

4) 使用 notificationManager.notify 实际上似乎删除了我之前设置的通知。实际上没有设置新的通知。所以我的应用程序在执行此行后没有通知。

private void setupStartForeground() {   

int id = 11;

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

Notification note = new Notification.Builder(this)
.setContentTitle("TEST1")
.setContentText("TEST2")
.setContentIntent(pi)
.build();
note.flags|=Notification.FLAG_NO_CLEAR;

startForeground(id, note);

//NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//notificationManager.notify(id, note);
}

最佳答案

我遇到了同样的问题,通过将正确的资源设置为 setSmallIcon 解决了这个问题。设置图标是强制性的,如果不这样做,默认通知 title='My App' 和 text='MyApp is running。点击以获取更多信息或停止 Activity 。被展示。这是我使用的代码:

       Resources res   = this.getResources();
String pkgName = this.getPackageName();
int resId;
resId = res.getIdentifier("myIcon", "drawable", pkgName);
int resId =
Notification note = new Notification.Builder(this)
.setSmallIcon(resId)
.setContentTitle("TEST1")
.setContentText("TEST2")
.setContentIntent(pi)
.build();

关于android - 单击通知未启动由服务创建的我的 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740312/

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