文章评分
次,平均分 :
在开发王柏元的博客客户端时,为了加入新文章发布时实现客户端通知的效果,极客人采用了百度云推送平台,使用百度官方提供的SDK和demo成功实现。服务端发送通知时,客户端会执行onNotificationClicked 回调函数。该函数的形参分别为
- Context context:上下文
- String title:通知标题
- String description:通知内容
- String customContentString:自定义字段,通过json可获取服务端的传值。
安卓端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@Override public void onNotificationClicked(Context context, String title, String description, String customContentString) { Intent intent = new Intent(); intent.setClass(context.getApplicationContext(), SinglePostActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); String postId=""; try { JSONObject jso=new JSONObject(customContentString); postId=jso.getString("postId"); } catch (JSONException e) { e.printStackTrace(); } intent.putExtra(SinglePostFragment.ARG_ITEM_ID,postId); intent.putExtra(SinglePostFragment.ARG_POST_TITLE, title); // context.startActivity(intent); context.getApplicationContext().startActivity(intent); String notifyString = "通知点击 title=\"" + title + "\" description=\"" + description + "\" customContent=" + customContentString; Log.d(TAG, notifyString); } |
服务器端代码需要引入sdk.php,关键是服务器要支持cUrl模块。我的阿里云免费主机就不支持,后来我消息推送的代码放在景安的虚拟主机上,通过阿里云调用URL的方式执行在景安主机上的PHP文件,解决了这一问题。
PHP服务器端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function newBlogPushtoAPP($post_ID){ global $post; require_once '../sdk.php'; // 创建SDK对象. $sdk = new PushSDK(); // message content. $message = array ( // 消息的标题. 'title' => $post->post_title, // 消息内容 'description' => $post->content, //自定义字段。数组类型 "custom_content"=> array( "postId"=> $post->ID ) ); // 设置消息类型为 通知类型. $opts = array ( 'msg_type' => 1 ); // 向目标设备发送一条消息 $rs = $sdk -> pushMsgToAll($message, $opts); } add_action ( 'publish_post', 'newBlogPushtoAPP', 1); |

关注我的微信,获取文章更新
如果你觉得这篇文章对你有用,可以点击下面的“赞助作者”打赏作者!
转载注明原文出处:王柏元的博客>>https://wangbaiyuan.cn/baidu-cloud-push-new-blog-articles-remind.html
小白一看代码就头大。