Notifications not pushed with android 10

Viewed 916

I am working on Android App with Firebase notification service FCM. and I recives notification successfully. but after I recive it is not showin in Android 10 (it work in 9 and 8 and smaller versions).

This is my showing notification code, I dont know what I missed. I check about if there is any changed with 10 but I dont see that...

My method:

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

}

Any help please? and Thanks

1 Answers

The ship has sailed, but:
worked skeleton (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);
}

}

Related