繁体   English   中英

通知未显示 - 已设置频道以及小图标、标题和文本

[英]Notification not showing - already set chennel as well as small icon, title and text

我一直在我的其他应用程序上使用通知,它工作得很好。 我也实现了channelId ,但是当我对新应用程序使用类似的代码时,通知没有显示。 没有错误报告。

以下是我正在使用的代码

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

        Intent notIntent = new Intent(context, hk.class);
//        notIntent.setAction(BuildConfig.APP_ID + ".inspire");
        notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendInt = PendingIntent.getActivity(context, ALARM_REQUEST_CODE,
                notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder mBuilder =
                new Notification.Builder(context)
                        .setContentIntent(pendInt)
                        .setSmallIcon(R.drawable.spl)
//                        .setLargeIcon()
                        .setContentTitle(intent.getStringExtra("not_title"))
                        .setContentText(intent.getStringExtra("not_text"))
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setDefaults(Notification.DEFAULT_VIBRATE)
                        .addAction(android.R.drawable.ic_menu_share, "Share", PendingIntent.getActivity(context, 0,
                                new Intent().setAction(Intent.ACTION_SEND).putExtra(Intent.EXTRA_TEXT,
                                         "\n\n" + "-His Holiness Bhakti Rasamrita Swami\n\n").setType("text/plain"), 0))
                        .setWhen(System.currentTimeMillis())
                        .setStyle(new Notification.BigTextStyle().bigText(intent.getStringExtra("big_text")));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("daily", "Daily Nectar", NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("Daily Nectar");
            channel.enableLights(true);
            mNotificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId("chId");
        }
        mNotificationManager.notify(ALARM_REQUEST_CODE, mBuilder.build());
        Log.d("AlarmReceiver", "Notification Created"); //this log is printed in console 

我已经使用日志进行了测试,因此我可以确保调用此函数,因此警报没有问题。

奇怪的是,它也不会抛出任何错误,并且其他应用程序上非常相似的代码运行良好。 所以,我检查了这个应用程序的通知设置,发现通知设置也被启用了。

无法检测是什么问题。 感谢您的帮助。

有一个愚蠢的错误。

注意下面的代码

        NotificationChannel channel = new NotificationChannel("daily", "Daily Nectar", NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("Daily Nectar");
        channel.enableLights(true);
        mNotificationManager.createNotificationChannel(channel);
        mBuilder.setChannelId("chId");

传递给新 NotificationChannel 的通道 id 和构建器上设置的通道 id 不同。 更新期间错误引入的错误。

快乐编码...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM