Android通知无法在Android 6.0(Marshmallow)上运行

问题描述 投票:5回答:3

我正在Android上实现本地通知,我遇到的问题是它们没有出现在Android 6.0(三星S7)上。我正在寻找解决方案,但我找不到任何解决这个问题的方法。我在正确的res / drawable文件夹中有图标,我也定义了通知标题,文本,铃声(原始文件夹)但它没有显示...有我的代码:

    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);

有没有其他人遇到过这个问题?任何帮助,将不胜感激。谢谢。

android notifications android-notifications android-6.0-marshmallow
3个回答
3
投票

新通知有一些变化。新的NotificationCompat.Builder(this)已被弃用,需要上面的NotificationChannelfor 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());

0
投票

必须启用Google Play服务才能在您的Android设备上接收推送通知。如果启用了Google Play服务并且常规故障排除步骤尚未解决问题,则可能需要重置应用。要重置应用程序,请转到设置→应用程序→PagerDuty,然后点击清除数据。

对于Android 6.0及更高版本,请确保将应用程序设置为优先级模式下的优先级应用程序。

对于Android 6.0及更高版本,请检查应用程序是否被Doze模式静音。


0
投票

检查android版本相应地设置> 6.0的图标和其他。对于6.0版本,我们需要白色背景图标。

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 ;
}
© www.soinside.com 2019 - 2024. All rights reserved.