繁体   English   中英

Android为什么通知无法在Oreo版本以下运行?

[英]Android Why Notification is not working on below Oreo version?

我是从android文档的这个URL引用此通知 尝试构建一个简单的通知,但似乎此代码适用于oreo及以上版本。 但是我应该如何处理以下版本的通知?

下面是我在单击按钮时运行的代码:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, PRIMARY_CHANNEL)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Much longer text that cannot fit one line...")
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText("Much longer text that cannot fit one line..."))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.feroz_channel_name);
            String description = getString(R.string.feroz_channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager1 = getSystemService(NotificationManager.class);
            notificationManager1.createNotificationChannel(channel);
        }
// notificationId is a unique int for each notification that you must define
        notificationManager.notify(123, mBuilder.build());

它可以在android oreo和pie版本上正常工作,但是如何为以下版本提供呢?

以下是我在以下奥利奥版本中运行上述代码时遇到的异常:

Process: sample.androido.com.myapplication, PID: 14596
    android.app.RemoteServiceException: Bad notification posted from package sample.androido.com.myapplication: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=sample.androido.com.myapplication id=0x7f0f0000) visible user=0 )
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

将小的notif图标放入可绘制的文件夹中。

为了提供对所有Android版本的支持,开发人员应该:将Android 3.0及更高版本的状态栏图标放在可绘制对象中...

检查文件

使用这种类型的通知。

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        Notification builder = new NotificationCompat.Builder(ctx, 
  notification.CHANNEL_ID_ONE)
                .setContentTitle(title)
                .setContentText(Artist)
                .setLargeIcon(bitmap1)
                .setSmallIcon(R.drawable.back)
                .addAction(R.drawable.play,"back",pi)
                .addAction(R.drawable.play,"play",pendingIntent)
                .addAction(R.drawable.play,"next",PI)
                .build();
        NotificationManager notificationManager = (NotificationManager) 
 ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(111, builder);
    }else{
        Notification notification = new NotificationCompat.Builder(ctx)
                .setContentTitle(title)
                .setContentText(Artist)
                .setLargeIcon(bitmap1)
                .setSmallIcon(R.drawable.back)
                .addAction(R.drawable.play,"back",pi)
                .addAction(R.drawable.play,"play",pendingIntent)
                .addAction(R.drawable.play,"next",PI)
                .build();
        NotificationManager notificationManager = (NotificationManager) 
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(111, notification);
    }

您知道如何在奥利奥(Oreo)中设置通知,因此else块的编码非常适合您,它一定会对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM