【问题标题】:Notification crashing on emulator and device通知在模拟器和设备上崩溃
【发布时间】:2019-05-20 15:32:53
【问题描述】:

我正在尝试在选中复选框并单击按钮以更新记录时推送通知。但是,当单击此按钮时,它会使运行低于 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 的任何设备版本上崩溃。

【问题讨论】:

  • 可以添加崩溃的logcat吗?

标签: java android push-notification notifications


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-07-09
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    相关资源
    最近更新 更多