android api-23 Notification

package com.example.test.utils;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import com.example.test.MainActivity;
import com.example.test.R;

public class NotificationUtils {

    public static void showNotifications(Activity activity){

        NotificationManager notificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);

        String channel = "TEST_NOTIFICATION_CHANNEL_TYPE";
        String channelName = "test_channel_name";

        // Android o 以上的版本(api为26)在使用通知时,需要另外一个类–NotificationChannel。
        // 在Android o之后app每发送一个通知都需要依附一个NotificationChannel,每个notification对象必须发送到指定的NotificationChannel,如果找不到channel,就会报错。
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            NotificationChannel notificationChannel = new NotificationChannel(channel,channelName,NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        //而且26以后一定要设置channel,否则不起作用报错
        NotificationCompat.Builder builder = new NotificationCompat.Builder(activity,channel);
        builder.setSmallIcon(R.drawable.icon_6_1);
        builder.setTicker("111111");
        builder.setContentText("这里是内容");
        builder.setContentTitle("标题");
        builder.setSubText("子标题");
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_ALL);

        //自定义通知View的展示
        RemoteViews remoteViews = new RemoteViews(activity.getPackageName(),R.layout.activity_app_widget_layout);
        remoteViews.setImageViewResource(R.id.functionImage,R.mipmap.back);
        builder.setCustomContentView(remoteViews);

        //确定点击跳转页面
        Intent intent = new Intent(activity, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(activity,0,intent,0);
        builder.setContentIntent(pendingIntent);

        //唤醒通知
        notificationManager.notify(1,builder.build());
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠天515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值