推送通知在 android 工作室中不起作用
push Notification is not working in android studio
嗨,我需要添加推送通知以代表公司欢迎用户
这是我的代码,但它不起作用
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("BergEsapes").setContentText("Kindly Login Your Account").
setContentTitle("User Register Succesfully").setSmallIcon(R.drawable.appicon).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
我认为您没有创建通知通道。这就是为什么它不显示通知。 Android Oreo(8.0) 及以上要求您必须先创建一个频道。请检查下面的代码以便更好地理解。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"YOUR_APP_NAME",
NotificationManager.IMPORTANCE_DEFAULT
);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
完整解决方案如下:
private void showNotification(String message){
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"default")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Notification Title")
.setContentText(message)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"YOUR_APP_NAME",
NotificationManager.IMPORTANCE_DEFAULT
);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
notificationManager.notify(NOTIFICATION_ID,builder.build());
}
嗨,我需要添加推送通知以代表公司欢迎用户 这是我的代码,但它不起作用
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("BergEsapes").setContentText("Kindly Login Your Account").
setContentTitle("User Register Succesfully").setSmallIcon(R.drawable.appicon).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
我认为您没有创建通知通道。这就是为什么它不显示通知。 Android Oreo(8.0) 及以上要求您必须先创建一个频道。请检查下面的代码以便更好地理解。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"YOUR_APP_NAME",
NotificationManager.IMPORTANCE_DEFAULT
);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
完整解决方案如下:
private void showNotification(String message){
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"default")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Notification Title")
.setContentText(message)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"YOUR_APP_NAME",
NotificationManager.IMPORTANCE_DEFAULT
);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
notificationManager.notify(NOTIFICATION_ID,builder.build());
}