android - 未使用 android 10 推送的通知

标签 android push-notification android-10.0

我正在使用 Firebase 通知服务 FCM 开发 Android 应用程序。我成功收到通知。但是在我收到它之后,它并没有在 Android 10 中显示(它适用于 9 和 8 及更小的版本)。

这是我的显示通知代码,我不知道我错过了什么。我检查了 10 是否有任何变化,但我没有看到...

我的方法:

private void sendNotification(String messageBody, String title) {
    Intent intent;
    if(type.equals("order_pending") || type.equals("order_declined")){
        intent = new Intent(this, myOrdersActivity.class);
    }else { intent = new Intent(this, MainActivity.class); }


    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int f =0;
    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }

 //   EventBus.getDefault().post(eventObject);

    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isActive", true)) {



        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        Intent dialogIntent = new Intent(this, MainActivity.class)
                .putExtra("ac", "1");
      //  sendBroadcast(dialogIntent);

        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        f =1;

       // startActivity(dialogIntent);
    }
    if(f==0) {
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

请问有什么帮助吗?
谢谢

最佳答案

船已经航行了,但是:
工作骨架(Java):

void sendNotif(int icon, String sText) {
String channel_id = "1002";
String sTitle = getString(R.string.app_name);

Intent notificationIntent = new Intent(this, activityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Android 8.0 (API level 26) and higher
    // https://developer.android.com/training/notify-user/build-notification
    CharSequence name = "statusbar";
    String description = getString(R.string.app_name);
    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);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id)
            .setSmallIcon(icon)
            /*.setContentTitle(sTitle)*/
            .setContentText(sText)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(contentIntent)
            .setAutoCancel(false);

    // notificationId is a unique int for each notification that you must define
    // and remember for delete notification
    notificationManager.notify(0, builder.build());
}
else {
    // Android 7.1 (API level 25) and lower
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification.Builder(getApplicationContext())
            .setTicker(sTitle)
            .setContentTitle(sTitle)
            .setContentText(sText)
            .setSmallIcon(icon)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent)
            .build();
    notification.flags |= Notification.FLAG_NO_CLEAR;
    // for BIG icon:
    //notification.contentView.setImageViewResource(android.R.id.icon, R.drawable.icon);
    mNotificationManager.notify(0, notification);
}
}

关于android - 未使用 android 10 推送的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60579690/

相关文章:

android - 当设备上创建的 Activity 轮换时,Youtube 播放器已被释放

iphone - 推送通知警报文本的最大长度是多少?

Android 隐藏自己的应用程序图标在某些 android 10 设备中不起作用

android - 跟踪推送通知并记录它们

android - 如何删除我在 Android Q 中创建的文件?

android - 在与 Kotlin 兼容的新 Android Q Preview SDK 中

java - 如何以编程方式在布局中居中进度条?

android - SQLITE Android 删除查询已执行但未删除该值。它在APP启动时再次显示

android - 启动时空白页

Angular 推送通知 - 避免存储重复的订阅对象