Android10 NotificationCompat.Builder使用

1. Deprecated constructor

@Deprecated 
public Builder(android.content.Context context)

2. Use of new methods

Reference link

参考链接:https://www.cnblogs.com/chunshu/p/10317960.html

Available constructor

public Builder(@NonNull android.content.Context context,
               @NonNull String channelId)
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
    

        Notification noti = new NotificationCompat.Builder(this, "channelid1")
                .setContentTitle("标题")
                .setContentText("Hello Content.")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //高版本需要渠道
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
    
    
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
        }

        manager.notify(1, noti);
        return super.onStartCommand(intent, flags, startId);
    }

3. Results

Notification in service

Guess you like

Origin blog.csdn.net/weixin_37627774/article/details/109204444