I have Implemented Local Notification in Android Application it is working in all mobile version well except Android Oreo (Android V8.0).
How can I create Notification in Android Application in android Oreo 8.0?
How to implement Notification in Android Oero?
- 5 years ago
You have to use NotificationChannel Class to implement Notification till you Android Oreo Version.
public void displayNotification(Context activity, int notiCode) { String title = activity.getString(R.string.app_name) + " is Active"; Intent tapIntent = new Intent(activity, DashboardActivity.class); notificationManager = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE); PendingIntent pendingIntent = PendingIntent.getActivity(activity, 1, tapIntent, 0); Notification.Builder builder = new Notification.Builder(activity) .setContentTitle(title) .setSmallIcon(R.drawable.notification_icon) .setAutoCancel(false) .setSound(null) .setContentIntent(pendingIntent).setOngoing(true) .setContentText("This is Content!"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel mChannel = new NotificationChannel(notiCode + "", title, NotificationManager.IMPORTANCE_HIGH); builder.setChannelId(notiCode + ""); mChannel.setSound(null, null); notificationManager.createNotificationChannel(mChannel); } if (Pref.getBoolean(activity, "showcurrentRej_pref")) { if (Pref.getInteger(activity, "messageCount_pref") > 0 || Pref.getInteger(activity, "callsCount_pref") > 0 || Pref.getInteger(activity, "notificationCount_pref") > 0) { builder.setStyle(new Notification.BigTextStyle(builder) .setBigContentTitle("Rejected Events") .bigText("Total Rejected Calls: " + Pref.getInteger(activity, "callsCount_pref") + "\nTotal Rejected SMS : " + Pref.getInteger(activity, "messageCount_pref") + "\nTotal Rejected Notification : " + Pref.getInteger(activity, "notificationCount_pref")) .setSummaryText("More")).setContentTitle(title); } } Notification notification = builder.build(); notificationManager.notify(notiCode, notification); }
- 5 years ago
You have to use NotificationChannel Class to implement Notification till you Android Oreo Version.
public void displayNotification(Context activity, int notiCode) { String title = activity.getString(R.string.app_name) + " is Active"; Intent tapIntent = new Intent(activity, DashboardActivity.class); notificationManager = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE); PendingIntent pendingIntent = PendingIntent.getActivity(activity, 1, tapIntent, 0); Notification.Builder builder = new Notification.Builder(activity) .setContentTitle(title) .setSmallIcon(R.drawable.notification_icon) .setAutoCancel(false) .setSound(null) .setContentIntent(pendingIntent).setOngoing(true) .setContentText("This is Content!"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel mChannel = new NotificationChannel(notiCode + "", title, NotificationManager.IMPORTANCE_HIGH); builder.setChannelId(notiCode + ""); mChannel.setSound(null, null); notificationManager.createNotificationChannel(mChannel); } if (Pref.getBoolean(activity, "showcurrentRej_pref")) { if (Pref.getInteger(activity, "messageCount_pref") > 0 || Pref.getInteger(activity, "callsCount_pref") > 0 || Pref.getInteger(activity, "notificationCount_pref") > 0) { builder.setStyle(new Notification.BigTextStyle(builder) .setBigContentTitle("Rejected Events") .bigText("Total Rejected Calls: " + Pref.getInteger(activity, "callsCount_pref") + "\nTotal Rejected SMS : " + Pref.getInteger(activity, "messageCount_pref") + "\nTotal Rejected Notification : " + Pref.getInteger(activity, "notificationCount_pref")) .setSummaryText("More")).setContentTitle(title); } } Notification notification = builder.build(); notificationManager.notify(notiCode, notification); }
- 2 years ago
Here is the Notification Receiver Class
public class FirebaseMessageReceiver extends FirebaseMessagingService { String channel_id = ""; @Override public void onMessageReceived(RemoteMessage remoteMessage) { //handle when receive notification via data event channel_id = getString(R.string.default_notification_channel_id); String title = "Notification"; String messageStr = "We are missing you, Please check our latest Offers to grow more."; String imagePath = "https://www.glassdoor.com/employers/app/uploads/sites/2/2021/03/GoogleDrive_640X469_How-to-Rescind-an-Offer-Letter-02.png"; if (remoteMessage.getData().size() > 0) { title = remoteMessage.getData().get("title"); messageStr = remoteMessage.getData().get("message"); imagePath = remoteMessage.getData().get("Image"); isLogPrintValue("NotificaXtionsData", "" + remoteMessage.getData().toString()); } //handle when receive notification if (remoteMessage.getNotification() != null) { title = remoteMessage.getNotification().getTitle(); messageStr = remoteMessage.getNotification().getBody(); isLogPrintValue("NotificaXtions_Data", "" + remoteMessage.getData().toString()); } showNewNotification(title, messageStr, imagePath); } public void showNewNotification(String title, String bodyString, String image) { Intent intent = new Intent(getApplicationContext(), SplashActivity.class); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String NOTIFICATION_CHANNEL_ID = channel_id; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_DEFAULT); //Configure Notification Channel notificationChannel.setDescription(title); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.GREEN); notificationChannel.setVibrationPattern( new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400} ); notificationChannel.enableVibration(true); notificationManager.createNotificationChannel(notificationChannel); } long notificatioId = System.currentTimeMillis(); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notificationBuilder.setSmallIcon(R.drawable.notification_icon); notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.boy)); notificationBuilder.setContentTitle(checkString(title)); notificationBuilder.setAutoCancel(true); notificationBuilder.setContentText(checkString(bodyString)); notificationBuilder.setSound(alarmSound); if (image != null && !image.isEmpty()) { Bitmap bitmap = getBitmapfromUrl(image); notificationBuilder.setStyle( new NotificationCompat.BigPictureStyle() .bigPicture(bitmap) .setSummaryText(bodyString)); } else { notificationBuilder.setContentText(bodyString); notificationBuilder.setStyle((new NotificationCompat.BigTextStyle().setSummaryText(bodyString))); } notificationBuilder.setAutoCancel(true); notificationBuilder.setContentIntent(pendingIntent); notificationBuilder.setWhen(notificatioId); notificationBuilder.setPriority(Notification.PRIORITY_HIGH); notificationManager.notify((int) notificatioId, notificationBuilder.build()); } public Bitmap getBitmapfromUrl(String imageUrl) { try { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); return BitmapFactory.decodeStream(input); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
Hot Questions