Android 通知未在一台设备上显示

Android Notifications not showing up in one device

通知出现在 2 台设备(小米、Vivo)中,均 >26 SDK。但它没有出现在我的三星设备上,它也是 >26。

这就是我实现通知的方式

private NotificationManager mNotificationManager1;

public void CreateMessage(String name,String message, Context context){
        SharedPreferences prefs = context.getSharedPreferences(Activity.class.getSimpleName(), Context.MODE_PRIVATE);
        int notificationNumber = prefs.getInt("notificationNumber", 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context, "Finderr_channel_id1");

        mNotificationManager1 =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            String channelId1 = "Finderr_channel_id1";
            NotificationChannel channel = new NotificationChannel(
                    channelId1,
                    "Finderr Message",
                    NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager1.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId1);
        }


        Intent ii = new Intent(context, NotificationHandler.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, ii, 0);

        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
        bigText.bigText(name + " sent you a message!");
        bigText.setBigContentTitle(name);
        bigText.setSummaryText(message);

        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setSmallIcon(R.drawable.ic_logo_small);
        mBuilder.setContentTitle(name);
        mBuilder.setContentText(message);
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        mBuilder.setStyle(bigText);

        mNotificationManager1.notify(notificationNumber, mBuilder.build());

        SharedPreferences.Editor editor = prefs.edit();
        notificationNumber++;
        editor.putInt("notificationNumber", notificationNumber);
        editor.commit();
    }

我已经使用共享首选项来跟踪我的通知计数,以便每条消息都与前一条消息叠加在一起。虽然,连 1 个通知都没有显示。

你调用了多少次CreateMessage方法? 此代码假定只为您的应用程序调用一次:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            String channelId1 = "Finderr_channel_id1";
            NotificationChannel channel = new NotificationChannel(
                    channelId1,
                    "Finderr Message",
                    NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager1.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId1);
        }

我还可以说,当我的应用程序试图通过永久通知启动前台服务时,三星设备出现了一些问题。详情见dontkillmyapp.com