未使用 android 10 推送通知

Hay*_*kad 6 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());
    }

}
Run Code Online (Sandbox Code Playgroud)

请问有什么帮助吗?谢谢

小智 0

船已经航行了,但是:
工作骨架(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);
}
Run Code Online (Sandbox Code Playgroud)

}