您的位置:首页 > 移动开发 > Android开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: