• 【Android】Parse Push快速入门指南



    前言

    很早就知道Parse服务,专为开发者提供服务端支撑,这又推出了Push服务,正好想用,实践发现速度快、简单、好用,这里顺便记录了一下使用心得。

    声明

    欢迎转载,但请保留文章原始出处:)
    博客园:http://www.cnblogs.com
    农民伯伯: http://over140.cnblogs.com 

    正文

    一、准备

    关于Parse的介绍,参考本文文末的链接(每月100万条免费Push)。首先你需要创建一个Parse账户,然后创建一个应用,最后下载SDK,解压并将jar拷贝到libs目录即可。

    官网:Parse Push 

    你可以尝试下这个下载链接:Parse-1.2.0.zip 

    二、 在你Application类的onCreate方法中调用Parse.initialize,如下:

     import android.app.Application;
     import com.parse.Parse;
     
     public class MyApplication extends Application {
     
       public void onCreate() {
         Parse.initialize(this, "your application id", "your client key");
       }
     
     }

      登录并创建好应用后,点击网页顶部的Quickstart就能进入本文的英文版本,并且已经给你填充好了applicationid和key,直接复制到程序里面即可。

    三、AndroidMainfest.xml设置

    这里设置权限、广播、Service等。 

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
     <service android:name="com.parse.PushService" />
     <receiver android:name="com.parse.ParseBroadcastReceiver">
       <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
         <action android:name="android.intent.action.USER_PRESENT" />
       </intent-filter>
     </receiver>

      注意别加错地方。

    四、订阅Push通知

    PushService.subscribe(this, "", YourActivity.class);
    PushService.setDefaultPushCallback(this, YourActivity.class);

      注意:

    a)、最后一个参数YourActivity.class,是指点击任务栏推送消息时接收处理的Activity,可以从getIntent中取到推送数据,例如 :

    com.parse.Channel:null 

    com.parse.Data:{"alert":"test","push_hash":"098f6bcd4621d373cade4e832627b4f6"}

    b)、这段代码也可以放到Application里面,放在Parse.initialize后面。

    c)、以广播的形式接收JSON数据:

        public class MyCustomReceiver extends BroadcastReceiver {
        private static final String TAG = "MyCustomReceiver";
         
          @Override
          public void onReceive(Context context, Intent intent) {
            try {
              String action = intent.getAction();
              String channel = intent.getExtras().getString("com.parse.Channel");
              JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
         
              Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
              Iterator itr = json.keys();
              while (itr.hasNext()) {
                String key = (String) itr.next();
                Log.d(TAG, "..." + key + " => " + json.getString(key));
              }
            } catch (JSONException e) {
              Log.d(TAG, "JSONException: " + e.getMessage());
            }
          }
        }
        

    五、其他

    a). 测试过程中发现,按照快速开发文档,最后点Send Temp Push没用,还以为失败了,直接进入应用后台的Push Notifications,点击Send a push,然后就可以发送消息了。发送成功一次后,后面都很快了。

    b). 注意要在后台Settings的Push notifications中启用Client push,设置为ON即可。 

    c). Parse Push支持IOS和Android的通知服务。 

    d). 1.3后好像要加以下代码:(2013-06-12更新)

    ParseInstallation.getCurrentInstallation().saveInBackground(); 

    六、相关文章

    Android推送实现方案探讨 

    Parse 是一个比较完善的专为您的IOS和Android应用而设计的后端平台

    谈谈移动应用开发的辅助服务 

    结束

     原计划试用一下极光推送,这里先试一下Parse的吧。现在搞APP的很多创业很多,其实专门做这种开发者服务也很好,更加容易实现盈利,同样最终服务于客户。创业到底是为了啥了?

  • 相关阅读:
    MongoDB-基础-limit-skip-sort
    MongoDB-基础-条件操作符
    mongodb-基础-update-remove
    Linq to sql-存储过程
    SQL Server-游标使用
    JavaScript-求时间差
    HTTP 错误 500.21
    .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
    HTTP 错误 500.21
    WebApi&MVC对比
  • 原文地址:https://www.cnblogs.com/over140/p/2968560.html
Copyright © 2020-2023  润新知