单击通知不启动由服务创建的PendingIntent

ABC*_*ABC 1 android android-service android-notifications android-pendingintent

因此,在开始在前台启动它之后,此代码将从我的服务内部运行.这也会创建将保持在那里的通知.我有很多问题/疑问:

1)单击通知仅打开应用信息,而不是我的PendingIntent.

2)我的内容标题和文字未设置.我得到"MyApp正在运行.请联系以获取更多信息或停止活动." 显示在通知中,而不是"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);
}
Run Code Online (Sandbox Code Playgroud)

小智 8

我遇到了同样的问题,并通过将正确的资源设置为setSmallIcon来解决问题.设置图标是必需的,如果不这样做,则标题='我的应用'和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();
Run Code Online (Sandbox Code Playgroud)