Java 如何在Android Studio手机上显示通知?
我想创建一个推送通知,管理员可以发送通知给所有用户。我找到了一个教程,并遵循它,但它不工作。我不确定我哪里做错了,但我得到的错误是Java 如何在Android Studio手机上显示通知?,java,android,push-notification,Java,Android,Push Notification,我想创建一个推送通知,管理员可以发送通知给所有用户。我找到了一个教程,并遵循它,但它不工作。我不确定我哪里做错了,但我得到的错误是 程序包“…”的开发人员警告未能在“null”频道上发布通知 在Oreo SDK之后,您必须创建通知通道才能显示通知,请检查此方法以供参考: /** * * @param context * @param title --> title to show * @param message --> details to show * @param
程序包“…”的开发人员警告未能在“null”频道上发布通知
在Oreo SDK之后,您必须创建通知通道才能显示通知,请检查此方法以供参考:
/**
*
* @param context
* @param title --> title to show
* @param message --> details to show
* @param intent --> What should happen on clicking the notification
* @param reqCode --> unique code for the notification
*/
public void showNotification(Context context, String title, String message, Intent intent, int reqCode) {
SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance(context);
PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT);
String CHANNEL_ID = "channel_name";// The id of the channel.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.notification_logo)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel Name";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(reqCode, notificationBuilder.build()); // 0 is the request code, it should be unique id
Log.d("showNotification", "showNotification: " + reqCode);
}
如何使用此方法:
int reqCode = 1;
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
showNotification(this, "Title", "This is the message to display", intent, reqCode);
您是否使用FCM或其他概念发送推送通知,您需要使用Firebase云消息等推送服务,但我建议您使用易于使用API调用实现的OneSignal。@Muruganathams否。我只想创建一个简单的通知,但我可以发送给所有人users@OMiShah我会检查的!谢谢你必须实现FCM消息服务参考此链接我必须在我的类的同一页上实现它吗?这是一个公共函数,你在参数中传递上下文,你可以使用此方法在Android系统中显示通知。我有问题。我的onCreate上有
notification()
。如何在onCreate上传递(上下文上下文、字符串标题、字符串消息、意图、int-reqCode)
我的通知中的?我已经添加了实现。如果您使用的是Firebase推送通知服务,那么您可以通过传递数据来显示收到的推送通知
int reqCode = 1;
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
showNotification(this, "Title", "This is the message to display", intent, reqCode);