最近单位要集成极光推送 而android这面需要走极光onMessage()方法自己去定义通知栏
下面开始实现
第一步 声明NotificationManager
public static NotificationManager notificationChannelManager;
第二步 初始化 通知栏方法 判断Build.VERSION.SDK_INT>26 才可以使用
notificationChannelManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
Uri huifang = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.huifang);
NotificationChannel huifangcannel = new NotificationChannel("huifang", "回访提示音", NotificationManager.IMPORTANCE_HIGH);
huifangcannel.setSound(huifang, att);//可以不调用 ,我测试没生效不知道为啥 我觉得可以注释
huifangcannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannelManager.createNotificationChannel(huifangcannel);
}
第三步 因为判断了 大于sdk大于26初始化通知栏 那也得兼容小于26得啊 小于26不用处理直接上方法 这个是SDK小于26 调用此方法
private void showTongzhi() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(MainActivity.this,"")
.setContentTitle("xxx") //设置标题
.setContentText("您的账号已经在其他设备登录,如果不是您本人操作请及时修改密码。") //设置内容
.setWhen(System.currentTimeMillis()) //设置时间
.setSmallIcon(R.mipmap.ic_launcher) //设置小图标 只能使用alpha图层的图片进行设置
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) //设置大图标
.setAutoCancel(true)
.setContentIntent(pi)
.build();
notificationChannelManager.notify(NOTIFICATION_ID, notification);
// index++;
}
以上看还得有个SDK大于26的方法
/*
* 简单的发送通知
*/
@SuppressLint("NewApi")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void showNotification(String qudaoid) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, qudaoid);
builder
// .setContent( new RemoteViews(getPackageName(),R.layout.item_new_notification))
.setContentTitle("xxx")
.setContentText("您的账号已经在其他设备登录,如果不是您本人操作请及时修改密码。")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知的优先级
.setOngoing(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
notificationChannelManager.notify(index, notification);
index++;
}
以上就完成了 下面开始调用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
showNotification("huifang");
} else {
showTongzhi();
}
notificationPlayVideo();
下面还有个声音提醒方法
public void notificationPlayVideo() {
//自定义声音
// Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.huifang);
//默认
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//类型通知 默认
Ringtone rt = RingtoneManager.getRingtone(getApplicationContext(), uri);
rt.play();
}
以上就完成了 欢迎大家点评