极光推送初遇

初次使用极光推送,文档没仔细看,就和服务端写了个简单的文本推送,测试后就这样用了。后来功能变更,发现不是那么回事。主要体现在,推送通知的处理不当,导致显示的是json内容。后来查看文档才发现,通知里面有规定的,是我们漏了很多东西,然后推荐给服务端查看后,经过商讨,重新更改才解决了问题。现在发出来与大家共勉。。。


通知栏显示json的原因是:服务端没有按文档要求,直接把推送内容,放在了alert里面。(alert是,通知是内容部分,所以就展示出来了)。


应该放在extra里面,这里面才是前端需要解析的json内容。



极光推送官方给出的Json格式:

{
   "platform": "all",
   "audience" : {
      "tag" : ["深圳", "北京"]
   },
   "notification" : {
 
      "android" : {
            "alert" : "Hi, JPush!",
            "title":"Send to Android",
            "builder_id":1,
            "extras" : { "newsid" : 321}
 
}, 
      "ios" : {
            "alert" : "Hi, JPush!",
            "sound":"default",
            "badge":"+1",
            "extras" : { "newsid" : 321}
      }
   },
   "options" : {
      "time_to_live" : 60,"apns_production":false
   }
}



JpushReceiver类的解释:


/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver,则: 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class JpushReceiver extends BroadcastReceiver {
    /** SDK 向 JPush Server 注册所得到的注册 全局唯一的 ID */
    private final String REGISTRATION_ID = JPushInterface.ACTION_REGISTRATION_ID;
    /** 收到了自定义消息 Push */
    private final String MESSAGE_RECEIVED = JPushInterface.ACTION_MESSAGE_RECEIVED;
    /** 收到了通知 Push */
    private final String NOTIFICATION_RECEIVED = JPushInterface.ACTION_NOTIFICATION_RECEIVED;
    /** 用户点击了通知 */
    private final String NOTIFICATION_OPENED = JPushInterface.ACTION_NOTIFICATION_OPENED;
    /** 在HTML中调用此函数后,会以广播的形式传递 ”params“ 到应用程序并触发客户端动作 */
    private final String RICHPUSH_CALLBACK = JPushInterface.ACTION_RICHPUSH_CALLBACK;
    /** 获取推送连接状态 */
    private final String CONNECTION_CHANGE = JPushInterface.ACTION_CONNECTION_CHANGE;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        // printBundle(bundle)
        String action = intent.getAction();

        if (REGISTRATION_ID.equals(action)) {
            // SDK 向 JPush Server 注册所得到的注册 全局唯一的 ID
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
        } else if (MESSAGE_RECEIVED.equals(action)) {// 自定义消息不会展示在通知栏,完全要开发者写代码去处理
            // 服务器推送下来的消息的标题(对应 API 消息内容的 title 字段;Portal 推送消息界上不作展示)
            String title = bundle.getString(JPushInterface.EXTRA_TITLE);
            // 服务器推送下来的消息内容(对应 API 消息内容的 message 字段;对应 Portal
            // 推送消息界面上的"自定义消息内容”字段。)
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            // 服务器推送下来的附加字段。这是个 JSON 字符串。(对应 API 消息内容的 extras 字段;对应 Portal
            // 推送消息界面上的“可选设置”里的附加字段)
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            // 服务器推送下来的内容类型(对应 API 消息内容的 content_type 字段)
            String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
            // 富媒体通消息推送下载后的文件路径和文件名
            String file = bundle.getString(JPushInterface.EXTRA_RICHPUSH_FILE_PATH);
            // 标识消息的 ID, 可用于上报统计
            String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);

            /** 处理推送的消息 */
            JpushProcess.processPushMessage(context, bundle);

        } else if (NOTIFICATION_RECEIVED.equals(action)) {// 接收到推送下来的通知,在这里可以做些统计,或者做些其他工作
            // 服务器推送下来的通知的标题(对应 API 通知内容的 title 字段;对应 Portal 推送通知界面上的“通知标题”字段)
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            // 服务器推送下来的通知内容(对应 API 通知内容的 alert 字段;对应 Portal 推送通知界面上的“通知内容”字段)
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            // 服务器推送下来的附加字段。这是个 JSON 字符串。(对应 API 通知内容的 extras 字段;对应 Portal
            // 推送消息界面上的“可选设置”里的附加字段)
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            // 通知栏的Notification ID,可以用于清除Notification
            int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            // 服务器推送下来的内容类型(对应 API 消息内容的 content_type 字段;Portal 上暂时未提供输入字段。)
            String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
            // 体通知推送下载的HTML的文件路径,用于展现WebView
            String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);
            // 富媒体通知推送下载的图片资源的文件名,多个文件名用 “,” 分开。 与
            // “JPushInterface.EXTRA_RICHPUSH_HTML_PATH” 位于同一个路径
            String fileStr = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_RES);
            String[] fileNames = fileStr.split(",");
            // 通知消息的 ID, 可用于上报统计
            String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);

        } else if (NOTIFICATION_OPENED.equals(action)) {// 用户点击打开了通知,定义用户点击后的行为
            // 打开自定义的Activity
            // Intent i = new Intent(context, TestActivity.class);
            // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // context.startActivity(i);

            // 保存服务器推送下来的通知的标题(对应 API 通知内容的 title 字段;对应 Portal
            // 推送通知界面上的“通知标题”字段.)
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            // 服务器推送下来的通知内容(对应 API 通知内容的alert字段;对应 Portal 推送通知界面上的“通知内容”字段)
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            // 服务器推送下来的附加字段。这是个 JSON 字符串(对应 API 消息内容的 extras 字段;对应 Portal
            // 推送消息界面上的“可选设置”里的附加字段)
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            // 通知栏的Notification ID,可以用于清除Notification
            int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            // 通知消息的 ID, 可用于上报统计
            String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);

            /** 处理点击推送的通知 */
            JpushProcess.processPushNotification(context, bundle);

        } else if (RICHPUSH_CALLBACK.equals(action)) {
            // 在HTML中调用此函数后,会以广播的形式传递 ”params“ 到应用程序并触发客户端动作

            // 根据extra的内容处理代码,比如打开新的Activity,打开一个网页等..
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
        } else if (CONNECTION_CHANGE.equals(action)) {// 获取推送连接状态
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
        }
    }

    /**
     * 打印所有的 intent extra 数据
     * 
     * @param bundle
     * @return
     */
    public 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 {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

    /**
     * 本地通知API
     * 
     * 本地通知API不依赖于网络,无网条件下依旧可以触发
     * 
     * 本地通知与网络推送的通知是相互独立的,不受保留最近通知条数上限的限制
     * 
     * 本地通知的定时时间是自发送时算起的,不受中间关机等操作的影响
     */
    public void localNotification(Context context) {
        JPushLocalNotification localNotification = new JPushLocalNotification();
        localNotification.setBuilderId(0);// 设置本地通知样式
        localNotification.setTitle("本地通知测试");// 设置本地通知的title
        localNotification.setContent("上宾本地通知测试");// 设置本地通知的content
        localNotification.setExtras(""); // 设置额外的数据信息extras为json字符串
        localNotification.setNotificationId(11111111);// 设置本地通知的ID
        localNotification.setBroadcastTime(System.currentTimeMillis() + 1000 * 60 * 10);// 设置本地通知触发时间
        // 增加一个本地通知
        JPushInterface.addLocalNotification(context, localNotification);
        // 移除指定的本地通知
        JPushInterface.removeLocalNotification(context, localNotification.getNotificationId());
        // 移除所有的本地通知
        JPushInterface.clearAllNotifications(context);
    }

}


推送的通知和消息的处理:


public class JpushProcess {

    /**
     * 处理点击推送的通知
     * 
     * @param context
     * @param bundle
     */
    public static void processPushNotification(Context context, Bundle bundle) {
        // 服务器推送下来的消息的标题(对应 API 消息内容的 title 字段;Portal 推送消息界上不作展示)
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        // 服务器推送下来的消息内容(对应 API 消息内容的 message 字段;对应 Portal
        // 推送消息界面上的"自定义消息内容”字段。)
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        // 服务器推送下来的附加字段。这是个 JSON 字符串。(对应 API 消息内容的 extras 字段;对应 Portal
        // 推送消息界面上的“可选设置”里的附加字段)
        String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
        // 服务器推送下来的内容类型(对应 API 消息内容的 content_type 字段)
        String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
        // 富媒体通消息推送下载后的文件路径和文件名
        String file = bundle.getString(JPushInterface.EXTRA_RICHPUSH_FILE_PATH);
        // 标识消息的 ID, 可用于上报统计
        String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);

        // 上报用户的通知栏被打开,或者用于上报用户自定义消息被展示等客户端需要统计的事件
        JPushInterface.reportNotificationOpened(context, msgId);
        // 清除所有Notification
        // JPushInterface.clearAllNotifications(context);
        // 清除Notification
        // JPushInterface.clearNotificationById(context, notificationId);

        setPushNotification(context, extra);
    }

    /**
     * 处理推送的消息
     * 
     * @param context
     * @param bundle
     */
    public static void processPushMessage(Context context, Bundle bundle) {
        // 服务器推送下来的消息的标题(对应 API 消息内容的 title 字段;Portal 推送消息界上不作展示)
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        // 服务器推送下来的消息内容(对应 API 消息内容的 message 字段;对应 Portal
        // 推送消息界面上的"自定义消息内容”字段。)
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        // 服务器推送下来的附加字段。这是个 JSON 字符串。(对应 API 消息内容的 extras 字段;对应 Portal
        // 推送消息界面上的“可选设置”里的附加字段)
        String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
        // 服务器推送下来的内容类型(对应 API 消息内容的 content_type 字段)
        String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
        // 富媒体通消息推送下载后的文件路径和文件名
        String file = bundle.getString(JPushInterface.EXTRA_RICHPUSH_FILE_PATH);
        // 标识消息的 ID, 可用于上报统计
        String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
        setPushMessage(context, message);
    }

    /**
     * 推送的通知
     * 
     * @param context
     * @param msg
     */
    private static void setPushNotification(Context context, String extra) {
        JSONObject object = null;
        try {
            object = new JSONObject(extra);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Bundle bundleData = new Bundle();
        if (object != null) {
            bundleData.putString("brandName", object.optString("brandName"));
            bundleData.putString("shopId", object.optString("shopId"));
            bundleData.putString("contactId", object.optString("id"));
        }
        String token = AppConfig.getData(context, AppConfig.APP_USER_TOKEN);
        if (!StringUtils.isEmpty(token)) {
            Intent intentData = new Intent();
            intentData.setClass(context, ContactInfoActivity.class);
            intentData.putExtras(bundleData);

            Intent[] intents = new Intent[2];
            Intent intentMain = new Intent();
            intentMain.setClass(context, MainActivity.class);
            intents[0] = intentMain;
            intents[1] = intentData;
            context.startActivities(intents);
        }

    }




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值