微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/android/430065.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐