天天看点

android 通知 Notification

private void showNotification2(Context context, int iid, String title, String text) {
    String id = "my_channel_01";
    String name = "我是渠道名字";
    NotificationManager notificationManager = (NotificationManager) MyApplication.mContext.getSystemService(NOTIFICATION_SERVICE);
    Notification notification = null;
    Toast.makeText(this, "qweqweqwewqewq", Toast.LENGTH_SHORT).show();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
        Log.i("log", mChannel.toString());
        notificationManager.createNotificationChannel(mChannel);
        notification = new Notification.Builder(this)
                .setChannelId(id)
                .setContentTitle("5 new messages")
                .setContentText("hahaha")
                .setSmallIcon(R.mipmap.ic_launcher).build();
    } else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyApplication.mContext)
                .setContentTitle("5 new messages")
                .setContentText("hahaha")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true)
                .setChannel(id);//无效
        notification = notificationBuilder.build();
    }
    notificationManager.notify(iid, notification);
}