0

I'm working with Android API 28. Through AlarmManager, I wanted to send push Notification at certain time in Service. So in the service, alarmManager sets specific duration for sending push notification.

I was trying to make it, so searched for some example. It worked at 'Marshmallow 6.0, and Nougat 7.0'. But not working at my Phone (Android Pie 9.0).

Only I found that working is 'onGoing' Notification, but I need removable notification from service.

How can I do? I need your help. Thank you for reading my question.

ADDED CODE

nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     tabletnoti_channel = new NotificationChannel(TABLETNOTI_ID, getString(R.string.noti), NotificationManager.IMPORTANCE_DEFAULT);
     tabletnoti_channel.setDescription(getString(R.string.app_name));
     tabletnoti_channel.enableLights(true);
     tabletnoti_channel.setLightColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
     tabletnoti_channel.enableVibration(true);
     tabletnoti_channel.setVibrationPattern(new long[]{100, 200, 100, 200});
     tabletnoti_channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

     nm.createNotificationChannel(tabletnoti_channel);
 }

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     Statebuilder = new Notification.Builder(getApplicationContext(), TABLETNOTI_ID);
 } else {
     Statebuilder = new Notification.Builder(getApplicationContext());
 }

 StateBuilder.setContentTitle(sharedPreferences.getString("connectDevice", "sample") + " " + getString(R.string.device_bat))
     .setContentText(getString(R.string.not_connected))
     .setOngoing(true)
     .setShowWhen(false)
     .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
     .setSmallIcon(R.drawable.ic_cloud_off)
     .setOnlyAlertOnce(true);

 if (sharedPreferences.getBoolean("show_noti", true)) nm.notify(STATE_NOTI_ID, StateBuilder.build());
 else nm.cancel(STATE_NOTI_ID);
Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
  • "How can I do?" – You just post a regular `Notification`. You don't necessarily even need a `Service` for that. You could do it from a `BroadcastReceiver`. – Mike M. Apr 10 '19 at 07:38
  • I want to post `Notification` at `service`, not app. So I need `service`. – Ericano Rhee Apr 10 '19 at 07:40
  • If i use `BroadcastReceiver`, maybe I should launch app. – Ericano Rhee Apr 10 '19 at 07:41
  • Why? What else is the `Service` doing? You don't need to launch an `Activity` to use a `BroadcastReceiver` as an alarm target, if that's what you're thinking. – Mike M. Apr 10 '19 at 07:41
  • Always service will be running, and check for blog - was posted. – Ericano Rhee Apr 10 '19 at 07:46
  • So service should post noti. – Ericano Rhee Apr 10 '19 at 07:46
  • Then, can I post nofication through `BroadcastReceiver`? `Service` or `Activity` weren't needed? – Ericano Rhee Apr 10 '19 at 07:50
  • Well, aside from how battery-unfriendly that probably is, you'll need to [edit] your question to provide a [mcve] that demonstrates the issue you're having on Pie. Real quick, though, are you creating and using a `NotificationChannel` on versions Oreo and up? If not, that's likely your problem: [Notification not showing in Android 8 Oreo](https://stackoverflow.com/q/43093260). – Mike M. Apr 10 '19 at 07:50
  • Nono, ongoing notification was perfectly posted, and I set `NotificationChannel` too. – Ericano Rhee Apr 10 '19 at 07:53
  • Then you'll need to provide us with a [mcve] that demonstrates the issue. – Mike M. Apr 10 '19 at 07:55

0 Answers0