当应用程序位于前景时,我正在尝试使用firebase显示通知。当我从服务器推送通知时调用onMessageReceived方法,但不显示通知 。
这是我的代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
Timber.d("FCM-From: " + remoteMessage.getFrom());
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (remoteMessage.getNotification() != null) {
Timber.d("FCM-Message Notification Body: " + remoteMessage.getNotification().getBody());
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), "CHANNEL_NOTIF")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("test")
.setContentText("test content");
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
Timber.d("FCM-Notif");
manager.notify(1, builder.build());
}
}
}
});
}
}
在我的logcat中,我可以看到:
FCM-Message Notification Body:202018105166
FCM-From:1049809400953
FCM-NOTIF
我关注https://developer.android.com/training/notify-user/build-notification.html#builder
我正在Oreo上运行
解决方案
在这里找到答案:Android foreground service notification not showing
这是一个奥利奥问题,感谢@Yashaswi N P
答案 0 :(得分:1)
下面提到的代码段行将帮助您解决问题:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new
NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance);
mChannel.setDescription(notification);
mChannel.enableLights(true);
mChannel.setLightColor(ContextCompat.getColor
(getApplicationContext(),R.color.colorPrimary));
notificationManager.createNotificationChannel(mChannel);
}
我在我的项目中使用了此代码段,它确实解决了这个问题。
答案 1 :(得分:1)
发生这种情况的原因是Android Oreo和更高的API级别。因此,您必须先创建通知通道,然后才能在Android 8.0及更高版本上发布任何通知,并且应在应用启动后立即执行此代码。重复调用此命令是安全的,因为创建现有的通知渠道不会执行任何操作。
并使用以下代码解决此问题:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationUtils notificationUtils;
private String title,message,click_action;
private String CHANNEL_ID = "MyApp";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
try {
JSONObject data = new JSONObject(remoteMessage.getData());
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
title = remoteMessage.getNotification().getTitle(); //get title
message = remoteMessage.getNotification().getBody(); //get message
click_action = remoteMessage.getNotification().getClickAction(); //get click_action
Log.d(TAG, "Notification Title: " + title);
Log.d(TAG, "Notification Body: " + message);
Log.d(TAG, "Notification click_action: " + click_action);
sendNotification(title, message,click_action);
}
}
private void sendNotification(String title,String messageBody, String click_action) {
Intent intent = new Intent(this, TargetActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.logo_heart)
.setContentTitle(title)
.setContentText(messageBody)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)// Set the intent that will fire when the user taps the notification
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(1, mBuilder.build());
createNotificationChannel();
}
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 = getString(R.string.app_name);
String description = getString(R.string.description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(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 = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
答案 2 :(得分:0)
在这里找到答案:Android foreground service notification not showing
需要使用Oreo处理频道
mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager);
mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor
(ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE);
mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build());
@TargetApi(26)
private void createChannel(NotificationManager notificationManager) {
String name = "FileDownload";
String description = "Notifications for download status";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(name, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.BLUE);
notificationManager.createNotificationChannel(mChannel);
}
感谢@Yashaswi N P
答案 3 :(得分:0)
我在清单文件中添加了以下权限。
我的问题解决了。
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />