在我的应用程序中,我想要显示通知,为此我使用了FireBase消息服务。
我在安卓 8 显示通知下写了下面的代码,但在安卓 8的上面没有显示任何通知!
我知道对于高于 android 8 的显示通知,我应该使用ChannelID,我为此编写代码,但不显示任何通知!
我的通知管理器类:
public class MyNotificationManager {
private Context mCtx;
private Uri soundUri;
private static MyNotificationManager mInstance;
private Intent intent;
private PendingIntent pendingIntent;
private NotificationManager mNotifyMgr;
public MyNotificationManager(Context context) {
mCtx = context;
}
public static synchronized MyNotificationManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title, String body) {
createNotificationChannel();
// main initialize
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotifyMgr = (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);
// Get General
intent = new Intent(mCtx, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (mNotifyMgr != null) {
mNotifyMgr.notify(0, getNotifyBuilder(title, body, pendingIntent).build());
}
}
private NotificationCompat.Builder getNotifyBuilder(String title, String body, PendingIntent pendingIntent) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, "utp_channel_1")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setSound(soundUri)
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true)
.setContentText(body)
.setContentIntent(pendingIntent);
return mBuilder;
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "app_channel";
String description = "app_channel_desc";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("app_channel_id", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = mCtx.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
MyFireBaseMessagingService class:
public class MyFireBaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotify(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
private void showNotify(String title, String body) {
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
myNotificationManager.displayNotification(title, body);
}
}
清单代码:
</service>
<service android:name=".utility.firebase.MyFireBaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我该如何解决它?
在 FirebaseMessagingService 类中,您只处理 通知有效负载,而不是数据有效负载。如果应用处于终止状态,并且 你发送通知它不调用 onReceive(( 方法,所以发送标题 和数据有效负载中的通知消息并生成通知。
尝试以下代码
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//if messages contains data payload (map of custom key values)
if(remoteMessage.getData().size() > 0){
//handle the data message here
sendNotification(remoteMessage);
}
//if notification payload
if (remoteMessage.getNotification() != null){
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage remoteMessage){
int notification_id = (int) System.currentTimeMillis();
NotificationManager notificationManager = null;
NotificationCompat.Builder mBuilder;
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String type = remoteMessage.getData().get("type");
//Set pending intent to builder
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
//Notification builder
if (notificationManager == null){
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
if (mChannel == null){
mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
mChannel.setDescription(CHANNEL_DESCRIPTION);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.GREEN);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
mBuilder.setContentTitle(title)
.setSmallIcon(R.drawable.ic_small)
.setContentText(body) //show icon on status bar
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setDefaults(Notification.DEFAULT_ALL);
}else {
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(title)
.setSmallIcon(R.drawable.ic_small)
.setContentText(body)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setDefaults(Notification.DEFAULT_VIBRATE);
}
notificationManager.notify(1002, mBuilder.build());
}
它将处理Android 8+和低于Android 8+。
试试这个方法
private void addNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default", "Channel name", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(R.mipmap.notiicon)
.setContentTitle(getResources().getString(R.string.app_name))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentText("Your text.....");
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
你也应该尝试一下,让我知道:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(
this, "my_channel_01").setSmallIcon(R.drawable.buy_gas)
.setContentTitle(getString(R.string.app_name)).setContentText("Title goes here").setContentIntent(contentIntent);
mBuilder1.setAutoCancel(true);
NotificationManager mNotificationManager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder1.setChannelId("my_channel_01");
CharSequence name = "My New Channel"; // The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance); //Create Notification Channel
channel.setDescription("Channel description");
mNotificationManager1.createNotificationChannel(channel);
}
mNotificationManager1.notify((int) System.currentTimeMillis(), mBuilder1.build());