Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification Not working on Android 6.0 (Marshmallow)

I'm implementing local notification on Android and I have the problem that they are not appearing on Android 6.0 (Samsung S7). I was searching for solutions, but I coulnd't find anything for this problem. I have the icon in the proper res/drawable folder, also I have defined a notification title, text, ringtone (raw folder) but it's not showing up... There is my code:

    Context acontext = getApplicationContext();

    PackageManager pm = acontext.getPackageManager();
    Intent notificationIntent = pm.getLaunchIntentForPackage(acontext.getPackageName());
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(acontext, 0, notificationIntent, 0);
    int notification_icon = acontext.getResources().getIdentifier("icon", "drawable", acontext.getPackageName());
    int notificationID = 0;

    // Build notification
    Notification noti = new Notification.Builder(acontext)
            .setContentTitle("Title")
            .setContentText("Incoming text")
            .setSmallIcon(notification_icon)
            .setContentIntent(pendingIntent)
            .setLights(Color.RED, 1, 1)
            .build();
    NotificationManager notificationManager = (NotificationManager) acontext.getSystemService(Context.NOTIFICATION_SERVICE);
    // hide the notification after its selected
    noti.sound = Uri.parse("android.resource://" + acontext.getPackageName() + "/raw/incoming");
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID, noti);

Did anyone else experience this problem? Any help would be appreciated. Thank you.

like image 818
Tünde Avatar asked Oct 17 '22 23:10

Tünde


2 Answers

There are some changes in new notification. new NotificationCompat.Builder(this) is deprecated and need NotificationChannelfor android oreo above. You can try this solution.

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
        Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);

        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
        bigText.bigText(verseurl);
        bigText.setBigContentTitle("Title");
        bigText.setSummaryText("Text in detail");

        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
        mBuilder.setContentTitle("Your Title");
        mBuilder.setContentText("Your text");
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        mBuilder.setStyle(bigText);

        NotificationManager mNotificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("notify_001",
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager.createNotificationChannel(channel);
        }

        mNotificationManager.notify(0, mBuilder.build());
like image 94
Md Imran Choudhury Avatar answered Oct 21 '22 01:10

Md Imran Choudhury


There are various layers involved in showing notifications in Android, Please first check if this works for other devices of same OS version. Normally these kind of issues are device specific not OS specific, Also check notification logs how to check notification logs in android ?

If issue is device specific, following can be handy tips:

  1. Check notifications not blocked for your app by device settings.
  2. Check power settings blocking non-priority notifications.
  3. If there are notifications in logs but not showing up, there can be some issue with your internal OS settings/configs. (believe me Android is not very clean OS)
  4. If you can, try factory resetting your device. (In my case this worked)
like image 44
humble_wolf Avatar answered Oct 21 '22 00:10

humble_wolf