0

This is the code that tries to create the notification:

NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();

PSTrip activeTrip = getActiveTrip();

Intent intent = new Intent(context, PSNewJourneyActivity.class);
intent.putExtra("id", activeTrip.getId());
intent.putExtra("userid", activeTrip.getOwner().getId() + "");
intent.putExtra("mode", "active");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

playCheckoutSound(context, false);

int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pIntent = PendingIntent.getActivity(context, iUniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "general");
mBuilder.setContentTitle(context.getString(R.string.passenger_name)).setContentText(context.getString(R.string.pit, "name")).setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentIntent(pIntent);

Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(iUniqueId, notification);

as you can see, I select the Channel ID, and I choose a uniqueID for the notification, but still, no luck. What am I doing wrong here? Also I tried calling this in the Looper.getMainLooper, don't know it that should affect it, but still no luck.

PS: My app has a foreground service, so it has a permanent notification, to let the user know that my app works in the background. Is this what is causing the issue?

EDIT:

I have added this before .notify, found in an answer:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
                    if (notificationChannel == null) {
                        int importance = NotificationManager.IMPORTANCE_HIGH;
                        notificationChannel = new NotificationChannel(channelId, channelDescription, importance);
                        notificationChannel.setLightColor(Color.GREEN);
                        notificationChannel.enableVibration(true);
                        notificationManager.createNotificationChannel(notificationChannel);
                    }
                }

My phone vibrates, but notification is still not shown.

SDK Version:

 compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
    minSdkVersion 17
    targetSdkVersion 27
}

Phone: Huawei Mate 10 Pro. Android 8.0

Logcat throws me this issue:

05-22 12:34:32.916: E/NotificationService(1184): No Channel found for pkg=nl.hgrams.passenger, channelId=general, id=124397682, tag=null, opPkg=nl.hgrams.passenger, callingUid=10233, userId=0, incomingUserId=0, notificationUid=10233, notification=Notification(channel=general pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
rosu alin
  • 5,674
  • 11
  • 69
  • 150

1 Answers1

1

If your app targets Android 8.0 then you must specify a notification channel.

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
    CHANNEL_ONE_NAME, 
    notifManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);

Check this link for creating a channel.

https://www.androidauthority.com/android-8-0-oreo-app-implementing-notification-channels-801097/

Mike
  • 4,550
  • 4
  • 33
  • 47
Ameer
  • 2,709
  • 1
  • 28
  • 44
  • testing this now – rosu alin May 22 '18 at 10:16
  • What is your target sdk version and compileSdkVersion – Ameer May 22 '18 at 10:19
  • PS: I also modified and tried with your code for creating the notification, but the same. – rosu alin May 22 '18 at 10:22
  • 1
    See the comment:To help you catch this error during testing, you may want to tell Android 8.0 to display a toast everytime your app tries to post a notification that doesn’t have an assigned channel: Open your device’s ‘Settings’ app. Navigate to ‘System > Developer options.’ Push the ‘Show notification channel warnings’ slider to the ‘On’ position. – Ameer May 22 '18 at 10:28
  • I don't have that. BUT if I check logcat, I get this issue: `05-22 12:34:32.916: E/NotificationService(1184): No Channel found for pkg=nl.hgrams.passenger, channelId=general, id=124397682, tag=null, opPkg=nl.hgrams.passenger, callingUid=10233, userId=0, incomingUserId=0, notificationUid=10233, notification=Notification(channel=general pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)` – rosu alin May 22 '18 at 10:35
  • how do I add my channel? – rosu alin May 22 '18 at 10:35
  • have you followed this?https://www.androidauthority.com/android-8-0-oreo-app-implementing-notification-channels-801097/ – Ameer May 22 '18 at 10:39