Android 单击通知不启动由服务创建的挂起内容

Android 单击通知不启动由服务创建的挂起内容,android,android-service,android-notifications,android-pendingintent,Android,Android Service,Android Notifications,Android Pendingintent,因此,这段代码是在我的服务开始在前台启动后从服务内部运行的。这也会创建一个通知,该通知将保留在那里。我有一些问题: 1) 单击通知只会打开应用程序信息,而不会打开我的挂起内容 2) 未设置我的内容标题和文本。我看到通知中显示的是“MyApp正在运行。触按可获取更多信息或停止活动”,而不是“TEST1 TEST2” 3) 注意.flags |=Notification.FLAG_NO_CLEAR似乎是多余的。我之前评论过它,在我尝试清除所有通知后,通知仍会保留在那里 4) 使用notificati

因此,这段代码是在我的服务开始在前台启动后从服务内部运行的。这也会创建一个通知,该通知将保留在那里。我有一些问题:

1) 单击通知只会打开应用程序信息,而不会打开我的挂起内容

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

3) 注意.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);
}

好的……我使用了这里的代码,第49-72行。

它已被弃用(如中不使用Notification.Builder),但它可以工作!我获得了正确的标题,单击通知将启动我的活动


不过,如果有人知道的话,我还是更喜欢一种不推荐的方法。

我也遇到了同样的问题,通过将正确的资源设置为setSmallIcon解决了这个问题。设置图标是必需的,如果未设置图标,则会运行带有title='My App'和text='MyApp'的默认通知。触按可了解更多信息或停止活动。'。以下是我使用的代码:

       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();

尝试设置通知标志:
notification.FLAG\u front\u SERVICE
notification.FLAG\u consuming\u EVENT
。我以前没有使用过
Notification.Builder
类来构造通知,因此它可能是在做一些假设,而不是为您设置这些信息。当我过去这样做的时候,我还设置了
Intent.FLAG\u FROM_BACKGROUND
标志在
pendingent
中。不,这仍然不起作用。仍然提供与以前相同的行为。是的,从2016年起,当您遇到默认通知问题时,
setSmallIcon
仍然是关键。谢谢你的回复:)