模拟器上通知正常,但在真实设备上无法接收到通知

3

我试图在三星Galaxy S7(Android 8.0)上测试我的应用程序,但通知没有出现。我已经检查过应用程序权限和电池设置,还有什么其他的可能性?

public void sendNotification(View view)
{

    @SuppressWarnings("deprecation")
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

    //creating the intent that’ll fire when the user taps the notification
    Intent intent = new Intent(getApplicationContext(), ViewActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    mBuilder.setContentIntent(pendingIntent);
    //making notification disappear after being clicked
    mBuilder.setAutoCancel(true);

    mBuilder.setSmallIcon(R.drawable.notification_icon);
    mBuilder.setContentTitle("TI connect");
    mBuilder.setContentText("New report has been added!  Total reports: " + repNo);
    mBuilder.setPriority(Notification.PRIORITY_MAX);

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

    mNotificationManager.notify(001, mBuilder.build());
}

当你的 API 大于 26.1.0 时,你需要使用 NotificationCompat 的新 Builder。https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder#notificationcompatbuilder - Crammeur
模拟器和真实设备的操作系统版本是否相同? - Zankrut Parmar
不,我在我的模拟器上使用的是21级 API。 - Mateusz Fijak
2个回答

1
在Android 8.0中需要Channal。您需要添加此内容。
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.app_name);
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                assert notificationManager != null;
                notificationManager.createNotificationChannel(mChannel);
            }

并且

String CHANNEL_ID = "my_channel_01";
notificationBuilder.setChannelId(CHANNEL_ID);

@surani,我遇到了类似的问题,我应该在哪里添加你的代码? - Michael Ng'ang'a Njoroge

1

尝试使用以下方法解决你的问题

 private void ShowNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle("Test Message")
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

并且在 strings.xml

 <string name="default_notification_channel_id">1</string>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接