[英]Notifications not showing
我试图向手机发送通知,但它没有发送
进口:
import androidx.core.app.NotificationCompat;
import android.content.Context;
import android.app.NotificationManager;
我的代码:
public void sendNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Title")
.setContentText("Content");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
}
为什么不显示? 是否需要意图? 渠道应该是什么? 谢谢!
如果您使用 firebase 发送通知,则需要使用 firebase 消息服务活动来接收通知。 并将数据添加到您的清单文件以接收通知。
如果您正在使用服务
从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个频道。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, 100);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel =
new NotificationChannel(
100,
"Media PlayBack",
NotificationManager.IMPORTANCE_LOW);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Title")
.setContentText("Content")
.setContentIntent(contentIntent);
Notification notification = builder.build();
startForeground(101, notification);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.