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

时间:2019-05-20 15:32:53

标签: 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的设备版本上崩溃。

1 个答案:

答案 0 :(得分:0)

您正在使用代码使用通道在api 24及以上版本上执行Notification,您应将此代码用于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());