java - 通知在模拟器和设备上崩溃

标签 java android push-notification notifications

我试图在选中复选框并单击按钮来更新记录时推送通知。但是,当单击此按钮时,它会使在低于 24 的版本上运行的所有模拟器和手机设备崩溃。

我收到了在 24 或更高版本上工作的通知,这表明 notificationcompat 构建器的实现是正确的,但它似乎不适用于任何低于 24 的设备版本。

通知生成器:

public void showNotification(String tvSeriesName) {
        String notificationText = "You watched '" + tvSeriesName +
                "'; how about telling others what you thought of it!";

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Watched TV series")
                .setContentText("You've watched:" + tvSeriesName)
                .setSmallIcon(R.drawable.ic_tv_24dp)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(notificationText))
                .setAutoCancel(true)
                .setSubText("")
                .setNumber(150)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();
        mNotificationManager.notify(111, notification);

我希望结果是弹出一个通知,显示您观看了 tvSeriesName 作为标题,然后如何告诉其他人您的想法,但正如我所说通知只会在任何低于 24 的设备版本上崩溃。

最佳答案

您正在使用代码在 api 24 及以上版本上使用 channel 执行通知,您应该将此代码用于 api 23 及以下版本:

 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        //if you have custom view 
        RemoteViews contentView = new RemoteViews(getPackageName(), 

        mBuilder.setSmallIcon(R.drawable.ic_accessibility_white_36dp);
        // if you have custom view 
        mBuilder.setContent(contentView);

        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(pendingIntent);
        // do not forget to mention the other info , title , text ,.....

        // unique id for NOTIFICATION_ID 
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

关于java - 通知在模拟器和设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56224076/

相关文章:

Android 网页 View : HTML Content is Overlapping

android - 在本地为 Android 构建 Phonegap 3.1 应用程序

ios - Push on App 未运行状态如何处理?

swift - 当应用程序处于后台时,推送通知 FCM 不会出现

java - 为什么卡片布局键作为对象传递?

android - 如何在 Android 上检查设备自然(默认)方向(即获取横向,例如摩托罗拉 Charm 或 Flipout)

java - android lollipop 上 sslsocket 中的 OutputStream 问题

Blackberry webworks 应用程序和推送通知

java - Jackson LocalDate/Time 反序列化

java - cucumber 步骤无法识别字符串