三星和小米中的通知显示此应用尚未向您发送任何通知

问题描述

我对通知有问题,该通知未在我的小米和三星设备上显示,但可在其他设备上使用。我已经尝试了人们推荐的方法,例如尝试自动启动,管理电池设置,但是这两种设备上都没有出现。我也尝试使用通知库,但结果相同。奇怪的是,当我检查通知设置然后单击该应用程序时,它显示如下所示

samsung

这是我的通知代码:

public void showNotification() {
    String appname = "App Name";
    String title = "Notification Title";
    String text = "This is the notification text";
    String iconUrl = "http://url.to.image.com/image.png";

    NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    TaskStackBuilder stackBuilder = TaskStackBuilder.from(MainActivity.this);
    stackBuilder.addParentStack(NewsActivity.class);
    stackBuilder.addNextIntent(new Intent(MainActivity.this,NewsActivity.class));
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    builder.setContentTitle(title).setContentInfo(appname).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.logo_wanda_flp)).setContentText(text).setContentIntent(pendingIntent);
    builder.setSmallIcon(R.drawable.logo_wanda_flp);

    notifyManager.notify("textid",123,builder.getNotification());
}

解决方法

如果未先注册通知频道,则该代码将无法在Android 8(API 26)及更高版本上运行。在此之前,在Android上,它可以正常工作。 以下某些代码段摘自here


创建通知频道

在尝试显示任何通知之前在活动中注册通知频道。另外,您可以在开始任何活动或服务之前在自定义Application类中注册它们(但是为了简单起见,此处不会显示)。

@Override
public void onCreate(Bundle savedInstanceState) {
    createNotificationChannel(); // Registering a notification channel
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}

private void createNotificationChannel() {
    // Create the NotificationChannel,but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

发送通知

public void showNotification() {
    // ...
    // NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    // Replace with line below.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,CHANNEL_ID);
    // ...
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...