Android极光推送自定义消息
2017-05-14 10:17
513 查看
集成极光推送时,通知可以不做任何操作就可以使用,而自定义消息却必须自己去处理消息,写一个广播来接受极光推送下来的消息并进行处理
接收消息
if(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
L.d("接受到推送下来的自定义消息");
showNotification(context, bundle);
}
弹出通知
/**
* 在状态栏显示通知
*/
private void showNotification(Context context,Bundle bundle){
//通知栏标题
String contentTitle=bundle.getString(JPushInterface.EXTRA_TITLE);
// 通知栏内容
String contentText=bundle.getString(JPushInterface.EXTRA_MESSAGE);
// 创建一个NotificationManager的引用
NotificationManager notificationManager=(NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification =new Notification(R.mipmap.ic_zdb,
"测试", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
//FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
//FLAG_ONGOING_EVENT 通知放置在正在运行
//FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
//DEFAULT_LIGHTS 使用默认闪光提示
//DEFAULT_SOUNDS使用默认提示声音 //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
notification.defaults = Notification.DEFAULT_LIGHTS; //叠加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //闪光时间,毫秒
// 设置通知的事件消息
Intent notificationIntent =new Intent(context, LogoActivity.class); // 点击该通知后要跳转的Activity
notificationIntent.setAction(CLEAR_NOTI_ACTION);//点击后发送通知取消通知的action
PendingIntent contentItent=PendingIntent.getActivity(context,0,notificationIntent, 0);
notification.setLatestEventInfo(context, "测试", contentText, contentItent);
}
}
最后别忘了注册广播
<receiver android:name=".reciver.JPushReceiver"
android:enabled="true"
>
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="com.gyj.test" />
</intent-filter>
</receiver>
接收消息
if(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
L.d("接受到推送下来的自定义消息");
showNotification(context, bundle);
}
弹出通知
/**
* 在状态栏显示通知
*/
private void showNotification(Context context,Bundle bundle){
//通知栏标题
String contentTitle=bundle.getString(JPushInterface.EXTRA_TITLE);
// 通知栏内容
String contentText=bundle.getString(JPushInterface.EXTRA_MESSAGE);
// 创建一个NotificationManager的引用
NotificationManager notificationManager=(NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification =new Notification(R.mipmap.ic_zdb,
"测试", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
//FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
//FLAG_ONGOING_EVENT 通知放置在正在运行
//FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
//DEFAULT_LIGHTS 使用默认闪光提示
//DEFAULT_SOUNDS使用默认提示声音 //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
notification.defaults = Notification.DEFAULT_LIGHTS; //叠加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //闪光时间,毫秒
// 设置通知的事件消息
Intent notificationIntent =new Intent(context, LogoActivity.class); // 点击该通知后要跳转的Activity
notificationIntent.setAction(CLEAR_NOTI_ACTION);//点击后发送通知取消通知的action
PendingIntent contentItent=PendingIntent.getActivity(context,0,notificationIntent, 0);
notification.setLatestEventInfo(context, "测试", contentText, contentItent);
}
}
最后别忘了注册广播
<receiver android:name=".reciver.JPushReceiver"
android:enabled="true"
>
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="com.gyj.test" />
</intent-filter>
</receiver>
相关文章推荐
- Android之极光推送发送自定义消息
- Android 使用极光推送自定义消息推送效果
- Android 使用极光推送自定义消息打造个性的消息推送效果
- Android 使用极光推送消息详细介绍之自定义消息
- Android之极光推送发送自定义消息
- Android 极光推送获取自定义消息
- ios极光推送的自定义消息和registerID的获取
- .net平台借助第三方推送服务在推送Android消息(极光推送)
- Android自定义消息推送
- Android 退出登录账号后,极光推送还能推送消息到手机的问题的解决
- iOS极光推送之自定义消息
- Android 中使用极光推送消息详细介绍(一)
- android消息推送---JPush(极光推送)
- .net平台借助第三方推送服务在推送Android消息(极光推送) 转
- 极光推送能收到通知却收不到自定义消息
- iOS极光推送(接收后台消息和自定义消息)
- 学习使用Android极光消息推送
- .net平台借助第三方推送服务在推送Android消息(极光推送)
- .net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新
- 使用极光推送自定义消息打造个性的消息推送效果