基于Bmob 做json消息推送 通知功能的实现

前言:不知道大家有没有感觉写个代码总是要反复修改,反复调试,我前几天就是想修复一个高并发风险,后来知道那么写就是错的,之后开始调用Bmob推送的接口,后发现自己之前用项目包还是android.support,而Coogle大佬已经推行Androidx很久了,我又是落伍青年在后面感觉修改,现在很多开源源码已经都换成Androidx,如果不需要就会有兼容性我问题,尽管android studio 3.4版有一键替换,但那也改变我项目红彤彤的命运,只能调啊调……我用博客记录自己的学习过程,感觉真的让自己有一个不断思考的过程与总结,尽管离毕业还有一年,我也将这个项目做我的毕业设计去写,也将是我2019年国家级大创的结题,尽管学校很水,比赛也水,自己更水,我还是想奔着就业这条路,一往无前,我好想就业啊,不想再学校这么待着混着了……谁知道那条路就是对的那走自己想走的路,就是对的,我就爱走我想走的路。     ——2019.6.16 江南小记

如果有人如我一样导入现在的开源接口包,报错的话,请及时更新你的项目包。本来想写一篇迁移Androidx的博文,写了一半,删了,当时我都修改过一半前面的改的错误也忘了截取,这个坑,根据自己的项目慢慢改吧,尚不能说出一二三四五六,只能说正常情况导入androidx 步骤后,查看项目的.java 的导包,之后看布局xml控件,看自己运行调试报错的地方,重新调试。

要为辅导员助手做消息推送,通知(Notification)这个功能向用户发出一些提示信息,校内发通知。

Android迁移成功后,按Bmob 推送文档集成成功!!!

并测试了一下图文消息好用哦!

准备工作完成。效果图:

接下来就涉及json 数据解析。推送的json

{
"alert": "推送消息测试", 
"articleurl": "http://jn.philuo.com/2019/06/16/92c61d6940d0510080fdb95726250bd1.html"
}
public class PushInfo {

    /**
     * alert : 推送消息测试
     * articleurl : http://jn.philuo.com/2019/06/16/92c61d6940d0510080fdb95726250bd1.html
     */

    private String alert;
    private String articleurl;

    public String getAlert() {
        return alert;
    }

    public void setAlert(String alert) {
        this.alert = alert;
    }

    public String getArticleurl() {
        return articleurl;
    }

    public void setArticleurl(String articleurl) {
        this.articleurl = articleurl;
    }
}
//TODO 集成:1.3、创建自定义的推送消息接收器,并在清单文件中注册
public class PushMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String msg = intent.getStringExtra("msg");
        if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            Logger.i("客户端收到推送消息:" + msg);


            Gson gson = new Gson();
            PushInfo pushInfo = gson.fromJson(msg, PushInfo.class);

            String alert = pushInfo.getAlert();
            String articleurl = pushInfo.getArticleurl();

            Intent pendingIntent = new Intent(context, WebviewActivity.class);
            Bundle B=new Bundle();
            B.putString("name",alert);
            B.putString("url",articleurl);
            pendingIntent.putExtra("data",B);

            pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
            BmobNotificationManager.getInstance(context).showNotification(largeIcon, "校内通知", alert, articleurl, pendingIntent, NotificationManager.IMPORTANCE_MIN, NotificationCompat.FLAG_ONLY_ALERT_ONCE);

        }
        
    }
}
发布了71 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39131246/article/details/92407547