Android使用NotificationManager进行消息推送

首先,发送一个状态栏通知必须用到两个类:NotificationManager、Notification。 NotificationManager:是状态栏通知的管理类,负责发通知、清楚通知等。 NotificationManager是一个系统Service,必须通过getSystemService()方法来获取。 

NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
Notification:是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。 

1、直接上代码

开启相关权限

    
    
    
    
    
    
    
    
    
    
  NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder1 = new Notification.Builder(Welcome.this);

        builder1.setSmallIcon(R.drawable.ic_launcher); //设置图标
        builder1.setTicker("显示第二个通知");
        builder1.setContentTitle("第一个通知"); //设置标题
        builder1.setContentText("点击查看详细内容"); //消息内容
        builder1.setWhen(System.currentTimeMillis()); //发送时间
        builder1.setDefaults(Notification.DEFAULT_SOUND);//设置指示灯

        builder1.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
        builder1.setLights(Color.GREEN, 1000, 2000);
        builder1.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        builder1.setSmallIcon(R.drawable.ic_launcher);
        builder1.setAutoCancel(true);//打开程序后图标消失

        Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.music);
        builder1.setSound(path);
        Intent intent = new Intent(Welcome.this, NotificationClickReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Welcome.this, 0, intent, 0);
        builder1.setContentIntent(pendingIntent);
        Notification notification1 = builder1.build();

        notificationManager.notify(124, notification1); // 通过通知管理器发送通知

监听消息点击后的触发事件:首先创建NotificationClickReceiver类并继承BroadcastReceiver类

package com.example.online.retailers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 *
 * Created by Waitforyou on 2019-03-26.
 */
public class NotificationClickReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //todo 跳转之前要处理的逻辑
        Log.i("TAG", "userClick:我被点击啦!!! ");
        Intent newIntent = new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newIntent);
    }

}

2、悬挂式通知

//在 build()之前设置 .setFullScreenIntent()
   Notification builder = new NotificationCompat.Builder(Context);
    Notification notify = builder.setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(Notification.PRIORITY_DEFAULT)  //通知的优先级
            .setCategory(Notification.CATEGORY_MESSAGE)  //通知的类型
            .setContentTitle("通知")
            .setAutoCancel(true)
            .setContentIntent(pi)
            .setContentText("Heads - Up Notification on Android 5.0")
            .setFullScreenIntent(pi, true)  //不设置此项不会悬挂,false 不会出现悬挂
            .build();

3、折叠式通知

Notification builder = new NotificationCompat.Builder(Context);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.sina.com"));

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

    // 未下拉的样式 R.layout.collapsed
    RemoteViews collapsed = new RemoteViews(getPackageName(), R.layout.collapsed);
    collapsed.setTextViewText(R.id.collapsed_text, "关闭状态");

    //下拉后的样式R.layout.show
    RemoteViews show = new RemoteViews(getPackageName(), R.layout.show);


    Notification notify = builder.setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentIntent(pi)
            .setContentText("新浪微博")
            .setCustomContentView(collapsed)//下拉前
            .setCustomBigContentView(show)//下拉后
            .build();

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, notify);

4.锁屏通知

//通过 setVisibility() 方法设置即可
...
.setVisibility(VISIBILITY_PUBLIC)
.build();

 

你可能感兴趣的:(Android)