1、使用高德定位开启后台定位
1.1创建通知
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40@SuppressLint("NewApi") private Notification buildNotification() { Notification.Builder builder = null; Notification notification = null; if (android.os.Build.VERSION.SDK_INT >= 26) { //Android O上对Notification进行了修改,如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏 if (null == notificationManager) { notificationManager = (NotificationManager) BaseApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE); } String channelId = BaseApplication.getInstance().getPackageName(); if (!isCreateChannel) { NotificationChannel notificationChannel = new NotificationChannel(channelId, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); notificationChannel.enableLights(false);//是否在桌面icon右上角展示小圆点 notificationChannel.setLightColor(Color.BLUE); //小圆点颜色 /**是否显示桌面角标,默认显示*/ notificationChannel.setShowBadge(false); /** 设置通知出现时声音,默认通知是有声音的*/ notificationChannel.setSound(null, null); // 设置通知出现时的震动(如果 android 设备支持的话) notificationChannel.enableVibration(false); notificationManager.createNotificationChannel(notificationChannel); isCreateChannel = true; } builder = new Notification.Builder(BaseApplication.getInstance(), channelId); } else { builder = new Notification.Builder(BaseApplication.getInstance()); } builder.setSmallIcon(R.drawable.app_logo) .setContentTitle("title") .setContentText("正在后台运行") .setWhen(System.currentTimeMillis()); if (android.os.Build.VERSION.SDK_INT >= 16) { notification = builder.build(); } else { return builder.getNotification(); } return notification; }
注意在三星手机8.0上notificationChannel.setShowBadge(true);时,应用退出到后台会在应用桌面生成带数字1角标,不设置notificationChannel.setSound(null, null);的话,推出到后台有默认声音和震动,影响用户体验;
1.2、通过调用高德api 启动后台定位功能
复制代码
1
2client.enableBackgroundLocation(2001, buildNotification());
2、通知的基本使用
2.1如果是Android o以上系统,需要使用通知渠道,代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); String title = "消息提示"; String desc = "您有" + num + "条未读消息"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "default"; String channelName = "默认通知"; NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); channel.setShowBadge(true); notificationManager.createNotificationChannel(channel); }
2.2创建通知对象:
复制代码
1
2
3
4
5
6
7
8
9
10
11Notification notification = new NotificationCompat.Builder(context, "default") .setContentTitle(title) .setContentText(desc) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true) .setChannelId("default") .setNumber(num)//桌面角标数量 .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL) .build();
2.3发送通知
发送通知前,先取消上一次发送的通知,代码如下:
复制代码
1
2
3
4
5int notificationId =1; //取消掉上一条通知消息 notificationManager.cancel(notificationId); notificationManager.notify(notificationId, notification);
3、特别注意(Android o)
当如果创建通知的时候使用了 Builder(Context context, String channelId) 带有channelId的构造方法创建的通知,如下:
复制代码
1new NotificationCompat.Builder(getApplicationContext(),"default")
则发送通知的NotificationManager对象也需要使用channelld指定通知渠道,代码如下:
复制代码
1
2
3
4NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); // channel.setShowBadge(true); notificationManager.createNotificationChannel(channel);
否则发送的通知不会在通知栏显示,且桌面不显示角标。
当使用Builder(Context context)方法创建的通知,则创建manager对象也可以不用指定channeld,
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25protected void setNotification(Notification notification, int notificationId, Context context) { if (notification != null) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // String title = "消息提示"; // String desc = "您有" + 1 + "条未读消息"; // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // String channelId = "default"; // String channelName = "默认通知"; // NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); // channel.setShowBadge(true); // notificationManager.createNotificationChannel(channel); // } notificationManager.notify(notificationId, notification); } }
最后
以上就是深情小鸭子最近收集整理的关于Android通知应用的全部内容,更多相关Android通知应用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复