Android Notification Doesn't work on Android 6.0 (Marshmallow)

I am implementing a local notification on Android, and I have a problem that they do not appear on Android 6.0 (Samsung S7). I was looking for a solution, but I did not find anything for this problem. I have an icon in the corresponding res / drawable folder, I also defined the notification name, text, ringtone (raw folder), but it does not appear ... 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);

Has anyone else experienced this issue? Any help would be greatly appreciated. Thank.

+5
source share
4 answers

. NotificationCompat.Builder(this) NotificationChannel android oreo . .

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());
+3

Google Play push- Android. Google Play , reset. reset "" → "" → "PagerDuty" " ".

Android 6.0 , .

Android 6.0 , Doze.

0

Android. , . , . Android?

, :

  1. , .
  2. , .
  3. , , / . ( , Android )
  4. , . ( )
0
source

Check the version of Android, respectively, set the icon for> 6.0 and others. For version 6.0 we need a white background icon.

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
 icon = R.mipmap.your_logo_for_Lolipop;
}else{
 icon = R.drawable.your_logo_for_Kitkat ;
}
-1
source

Source: https://habr.com/ru/post/1658667/


All Articles