我在Android API 29上显示通知时遇到问题。尽管代码基于this教程,但执行代码时未显示通知。
MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
val notification =
NotificationCompat.Builder(this, NOTIFICATION_GROUP_LOCATION)
.setContentTitle("NoteIfication")
.setContentText("Note: Priority: ")
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_LOCATION,
"group location",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "notification channel for note reminders in app HyperNote"
}
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
with(NotificationManagerCompat.from(this)) {
notify(2134, notification)
}
}
}
答案 0 :(得分:1)
正如Mike M.指出的那样:
“看起来您可能正在使用两个不同的通道ID; NOTIFICATION_GROUP_LOCATION和NOTIFICATION_CHANNEL_LOCATION。您在NotificationCompat.Builder(this,...)中传递的ID必须与在NotificationChannel(...,“ group中传递的ID相同位置”,IMPORTANCE_DEFAULT)。”
更改ID后,将显示通知。
答案 1 :(得分:-1)
这是我从第一天起就用于本地通知的方法
private void sendNotification(Blog blog) {
String title=blog.getTitle();
String desc=blog.getDesc();
String img=blog.getImage();
String time =blog.getTimestamp();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("desc", desc);
bundle.putString("image", img);
bundle.putString("time", time);
Intent intent = new Intent(this, NavigationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("blog", bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_TAG,
intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL url = new URL(img);
Notification notification = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo_icon)
.setContentTitle(title)
.setContentText(desc)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(BitmapFactory.decodeStream((InputStream)url.getContent()))
.bigLargeIcon(null))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"OlaitexBlog notifications",
NotificationManager.IMPORTANCE_DEFAULT);
Objects.requireNonNull(notificationManager).createNotificationChannel(channel);
}
Objects.requireNonNull(notificationManager).notify(0 /* ID of notification */, notification );
}catch (Exception e){
e.printStackTrace();
}
}
问我您在这里不了解的内容