Notification android 8 and above
-
I used firebase (java) for remote notifications, when a new notification arrives a java class that extends FirebaseMessagingService class will be called.
Then i separate remote messages like title or etc... and send them to another class that i named GetExtra.
GetExtra is a singletone class:public class GetExtra { NotificationManagerCompat notificationManager; NotificationCompat.Builder builder; private static final GetExtra singleton = new GetExtra( ); private GetExtra() { } public static GetExtra getInstance( ) { return singleton; } int a=0; public void setMessageData(Context ctx, String title, String text, String postid ) { createNotyf(ctx,title,text,postid); } public void createNotyf(Context ctx, String title, String text, String postid) { Log.d("nnnnnnnnnnnn: " , title); int requestCode= (int) System.currentTimeMillis(); Intent notificationIntent = new Intent(ctx, GetNotifExtraService.class); notificationIntent.putExtra("postid", postid); PendingIntent pendingIntent = PendingIntent.getService(ctx, requestCode, notificationIntent,0); notificationManager = NotificationManagerCompat.from(ctx); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(ctx.getPackageName(),"new message!", NotificationManager.IMPORTANCE_DEFAULT); NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context. NOTIFICATION_SERVICE ); notificationManager.createNotificationChannel(channel); builder = new NotificationCompat.Builder(ctx ,channel.getId()); } else { builder = new NotificationCompat.Builder(ctx); } builder.setSmallIcon(R.drawable.ic_launcher_foreground) .setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle() .bigText(text)) .setContentIntent(pendingIntent) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_SOUND) .setColor(Color.GREEN) .setPriority(NotificationCompat.PRIORITY_MAX); notificationManager.notify(a,builder.build()); a=a+1; Log.d("nnnnnnnnnnnn: " , String.valueOf(a)); } }
In this class i create a local notification with custom operations from remote messages data.
All things works well in android 7 or older.
But in android 8 or above (although I wrote android 8 and above particular codes and used NotificationChannel) notifications can't made.I dont faced any warning or error in compile or running logs.
Only this warning is seen when compiling and i don't know if it's relevant:
Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details.
I tested this class in a new android studio project and there isn't any problem in android 8 with android studio builds.
-
I found that. NotificationManagerCompat and NotificationCompat are deprecated.
But i dont know android studio how resolved that.(i used same build tools and target and etc)The corrected code is as follows:
package my.package.name; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Notification; import android.graphics.BitmapFactory; public class GetExtra { NotificationManager notificationManager; Notification.Builder builder; private static final GetExtra singleton = new GetExtra( ); private GetExtra() { } public static GetExtra getInstance( ) { return singleton; } int a=0; public void createNotification(Context ctx, String title, String text, String postid) { int requestCode= (int) System.currentTimeMillis(); Intent notificationIntent = new Intent(ctx, GetNotifExtraService.class); notificationIntent.putExtra("postid", postid); PendingIntent pendingIntent = PendingIntent.getService(ctx, requestCode, notificationIntent,0); notificationManager = (NotificationManager) ctx.getSystemService(Context. NOTIFICATION_SERVICE ); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(ctx.getPackageName(),"new message!", NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(channel); builder = new Notification.Builder(ctx ,channel.getId()); } else { builder = new Notification.Builder(ctx); } builder.setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher)) .setContentTitle(title) .setContentText(text) .setDefaults(Notification.DEFAULT_SOUND) .setColor(0xFFFAFC77) .setContentIntent(pendingIntent) .setAutoCancel(true) .setPriority(Notification.PRIORITY_MAX); notificationManager.notify(a,builder.build()); a=a+1; } }