NotificationManager NotificationCompat NotificationChannel 自定义通知

兼容高版本和第办法通知方法
静态方法 :

    public static NotificationChannel initNotificationChannel(Context context,String id, String name){
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel =null;
        if (Build.VERSION.SDK_INT>=26)
        {
            channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
        return channel;
    }

    //获取通知
    public static NotificationCompat.Builder getNotification(Context context, NotificationChannel channel) {
        NotificationCompat.Builder builder;
        if (channel!=null && Build.VERSION.SDK_INT>=26)
        {
            builder=new NotificationCompat.Builder(context,channel.getId());
        }else {
            builder=new NotificationCompat.Builder(context,null);
        }
        return builder;
    }

代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Handler handler;
    private NotificationManager manager;
    private NotificationChannel channel;
    private NotificationCompat.Builder builder;
    private NotificationCompat.Builder notification;
    private RemoteViews remoteViews;
    private Notification build;
    private Notification build2;
    private js js;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler=new Handler();
        PNViewFind.Bind.init(this);

        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        channel = CXUtils.initNotificationChannel(this, "cx", "cheng_xing_net");

        builder=CXUtils.getNotification(this, channel);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("这是内容");
        builder.setContentTitle("标题");
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setOngoing(true);
        builder.setAutoCancel(true);
        Intent intent=new Intent(this, TestActivity.class);
        PendingIntent intent1=PendingIntent.getActivity(this,0,intent,0);
        builder.setContentIntent(intent1);

        build = builder.build();

        notification = CXUtils.getNotification(this, channel);
        notification.setSmallIcon(R.mipmap.ic_launcher);
        notification.setAutoCancel(true);
        notification.setContentIntent(intent1);

        Intent intent2 = new Intent("cx");
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, intent2, 0);
        remoteViews=new RemoteViews(getPackageName(), R.layout.notification);
        remoteViews.setOnClickPendingIntent(R.id.msg, pendingIntent);
        notification.setContent(remoteViews);

        build2 = notification.build();

        IntentFilter filter=new IntentFilter();
        filter.addAction("cx");
        js = new js();
        registerReceiver(js, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(js);
    }

    private class js extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(MainActivity.this, "接受了",Toast.LENGTH_SHORT).show();
            remoteViews.setTextViewText(R.id.msg, "修改文字");
            manager.notify(4, build2);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.use)
        {
            manager.notify(4, build2);
        }
        if (v.getId()==R.id.upd)
        {
            remoteViews.setTextViewText(R.id.msg, "修改文字");
            manager.notify(4, build2);
        }
    }

}

你可能感兴趣的:(NotificationManager NotificationCompat NotificationChannel 自定义通知)