通知未显示 – 已设置通知通道以及小图标、标题和正文。

huangapple go评论86阅读模式
英文:

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

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

奇怪的是,它也没有抛出任何错误,其他应用程序上的非常相似的代码也运行良好。因此,我检查了此应用程序的通知设置,并发现通知设置也已启用。

无法确定问题所在。感谢您的帮助。

英文:

I have been using notification on my other app and it is working just fine. I have also implemented channelId, but when I am using the similar code for new app, notification is just not showing up. No errors reported.

Following is the code I am using

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 

I have tested using Logs and thus I can ensure that this function is called, so no problem with alarm.

Strangely it doesn't throw any errors also and very similar code on other app is working well. So, I checked the notification setting for this app and found that notification settings are also enabled.

Unable to detect what is the problem. Thank you for the help.

答案1

得分: 1

有一个愚蠢的错误。

请注意以下代码

NotificationChannel channel = new NotificationChannel("daily", "每日花蜜", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("每日花蜜");
channel.enableLights(true);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("chId");

传递给新的NotificationChannel的通道ID和设置在构建器上的通道ID是不同的。在更新过程中错误地引入了一个bug。

祝编码愉快...

英文:

There was a silly mistake.

Note the following code

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

channel id passed to new NotificationChannel and channel id set on builder are different. A bug introduced mistakenly during the update.

Happy coding...

huangapple
  • 本文由 发表于 2020年10月4日 16:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64192352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定