安卓|Java 通知未出现在 API 25 及更低版本中



我有一个发送短信的Firebase推送通知。我的通知显示在 API 26 之后,但在较低的 API 上(当前使用 API 22 进行测试(并且消息已成功发送,但它们未显示在 (API 22( 设备上。可能是什么问题?

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
createNotificationChannel();
String messageTitle = remoteMessage.getNotification().getTitle();
String messageBody = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String dataMessage = remoteMessage.getData().get("message");
String dataFrom = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(click_action);
intent.putExtra("message", dataMessage);
intent.putExtra("from_user_id", dataFrom);
PendingIntent resultIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Personal Notifications";
String desc = "Include all the personal notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
notificationChannel.setDescription(desc);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}

可能是什么问题?

如果您有情况,当通知出现在状态栏中,但没有弹出浮动窗口时,您应该添加您的通知兼容.生成器

.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)

因为此类通知称为提醒通知,并且它应该具有高优先级,并在运行 Android 7.1(API 级别 25(及更低版本的设备上使用铃声或振动

问题是您在创建通知频道中定位了奥利奥及以上版本。

您定位奥利奥及以上位置的确切位置是:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)

发生这种情况的原因是因为下面列出的代码,因为它是在 API 级别 26 中添加的。您需要为较低级别找到替代版本。

notificationManager.createNotificationChannel(notificationChannel);

我前一阵子有这个。将createNotificationChannel()方法保留在主类中。但是,我建议使用静态方法创建一个不同的类(以创建通知(,然后在FirebaseMessagingService类中调用它。试试这个:

public class NotificationHelper {
public static void displayNotification(Context context,String title,String body){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,CHANNELID)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat managerCompat =  NotificationManagerCompat.from(context);
managerCompat.notify(1,mBuilder.build());
}
}

然后在FMService类中,只需在通知通道之后调用静态方法,如下所示:

public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
createNotificationChannel();
if(remoteMessage.getNotification()!=null){
String messageTitle = remoteMessage.getNotification().getTitle();
String messageBody = remoteMessage.getNotification().getBody();
NotificationHelper.displayNotification(getApplicationContext(),messageTitle,messageBody);
}

你应该没事的。

最新更新