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

我正在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,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);

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

最佳答案
通知有一些变化.新的NotificationCompat.Builder(this)已被弃用,需要NotifyChannelfor 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,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());

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...