打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
极光接受通知栏系统消息 文件
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.xgs.utils.UesrPreferencesUtil;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import cn.jpush.android.api.JPushInterface;

/**
* Created by Administrator on 2016/10/18.
*/

public class JPushReceiver extends BroadcastReceiver {

private static final String TAG = "JPush";

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
UesrPreferencesUtil uu = new UesrPreferencesUtil(context);
Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
//接收到 极光返回的regid 根据当前设备返回的 和使用者无关 有时可能返回失败 需自己判断
//可保存到自定sharePreferences文件
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
uu.put("regId", regId);
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);


} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
// 所有自定义的消息才会进入这个方法里

Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// processCustomMessage(context, bundle);

} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
// 所有的普通推送,都会进入到这个部分,并且Jpush自己会进行 Notification的显示
//可以根据接收到的 信息判断 直接执行相关操作 不需要点击


Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
// 此信息为在手机系统 通知栏 显示的 信息 可设置点后 跳转自定义页面
// notification点击打开,主要针对是普通的推送消息
//打开自定义的Activity
try {
Intent i = new Intent(context, PaymentActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);

} catch (Exception e) {
e.printStackTrace();
}


} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));


} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}

// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
Log.i(TAG, "This message has no Extra data");
continue;
}

try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();

while (it.hasNext()) {
String myKey = it.next().toString();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " + json.optString(myKey) + "]");
}
} catch (JSONException e) {
Log.e(TAG, "Get message extra JSON error!");
}

} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}

/**
* 124. * 可能经常用到的一点,获取附加的自定义的字段
* 125. *
* 126. * @param context
* 127. * @param bundle
* 128.
*/

private void processCustomMessage(Context context, Bundle bundle) {
// if (MainActivity.isForeground) {//检查当前软件是否在前台
// 利用JPushInterface.EXTRA_MESSAGE 机械能推送消息的获取

/**
* 通过输出日志我们知道extra是JSON格式的字符串
* 解析Json
*/
String mess = bundle.getString(JPushInterface.EXTRA_MESSAGE);
System.out.println("收到了自定义消息。消息内容是:" + mess);
// 可能经常用到的一点,获取附加的自定义的字段、
// 这个字符串就是Json的格式,用于自己的服务器给特定的客户端传递一些特定的属性和配置,
// 例如显示一些数字、特定的事件,或者是访问特定的网址的时候,使用extras
// 例如显示订单信息、特定的商品列表,特定的咨询网址

String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
System.out.println("收到了自定义消息。extra是:" + extra);

/**
* 自定义信息: 获取
* */
Map<String, Object> map = new HashMap<String, Object>();
if (extra != null) {

JSONObject jsonObject;
try {
jsonObject = new JSONObject(extra);
String key = jsonObject.getString("key");
map.put("key", key);
} catch (JSONException e) {
e.printStackTrace();
}
}
map.put("message", mess);

Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(calendar.getTime());
map.put("date", date);
//实体类装入了 信息 日期 和 详情
Data.data.add(map);
Intent intent = new Intent(context, MessageReceiedActivity.class);
Bundle budle = new Bundle();
budle.putString("all", Data.data.toString());
intent.putExtras(budle);
context.startActivity(intent);
}
}
 
需要在Application文件 
//增加激光推送
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
 
配置文件 权限设置 
<receiver
android:name="com.xgs.financepay.receiver.JPushReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required 用户注册SDK的intent-->
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required 用户接收SDK消息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required 用户接收SDK通知栏信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required 用户打开自定义通知栏的intent-->
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!--Optional 用户接受Rich Push Javascript 回调函数的intent-->
<action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category android:name="com.xgs.financepay" />
</intent-filter>
</receiver>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
消息推送
Android简明开发教程二十:Broadcast Receiver 短信触发示例 | 第...
Android 监听wifi广播的两种方式
Android中Bundle类的作用
基于android startActivityForResult的学习心得总结
“翻转静音”的Android实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服