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

时间:2019-10-21 14:35:20

标签: android firebase firebase-cloud-messaging

我有一个发送文本消息的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);
        }
    }

}

可能是什么问题?

3 个答案:

答案 0 :(得分:1)

如果情况允许,状态栏上会显示通知,但未弹出浮动窗口,则应在NotificationCompat.Builder中添加

FragA

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

答案 1 :(得分:0)

问题是您在createNotificationsChannel中定位了Oreo及更高版本。

您定位到奥利奥及以上的确切地点是:

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

之所以会发生这种情况,是因为自从API级别26中添加了以下代码后,下面列出了这些代码。您将需要为较低级别找到替代版本。

notificationManager.createNotificationChannel(notificationChannel);

答案 2 :(得分:0)

我刚才有这个。将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类中,仅在通知通道之后调用static方法,如下所示:

 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);
        }

你应该没事。