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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/android/430065.html

猜你在找的Android相关文章