通知只能在模拟器上运行,而不能在真实设备上运行

时间:2018-08-03 07:23:45

标签: android android-notifications

我正在尝试在Samsung 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());
}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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>