【Android】Parse 開發筆記(4)—— Push Notifications(上)



前言

合理的使用Push服務,能極大提高用戶活躍度,本博《Parse Push快速入門指南》僅簡單介紹和使用了一下Parse的推送服務,這里介紹更多的使用方法和技巧。系列文章分兩篇來介紹Push服務,分別針對所有用戶(上)和渠道定制用戶(下)。

 

聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com

農民伯伯: http://over140.cnblogs.com 

 

正文

一、系列

1.1 【Parse】開發筆記(1)—— 准備

1.2 【Parse】開發筆記(2)—— 從Mysql導入數據到Parse Data

1.3 【Parse】開發筆記(3)—— 實現查找附近的功能(LBS) 

  

二、准備

2.1 官網的Android Push Notifications

https://www.parse.com/tutorials/android-push-notifications

2.2  【Android】Parse Push快速入門指南

http://www.cnblogs.com/over140/archive/2013/03/19/2968560.html  

 

三、 功能

3.1 准備

AndroidManifest.xml

     < 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"   />


     < application
        
android:name ="com.nmbb.lol.LOLApplication"
        android:allowBackup
="true"
        android:icon
="@drawable/app_icon"
        android:label
="@string/app_name"
        android:theme
="@style/AppTheme"   >
         < 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 >
        
         < receiver  android:name =".receiver.ReceiverPush"   >
             < intent-filter >
                 < action  android:name ="com.nmbb.lol.push"   />
             </ intent-filter >
         </ receiver >
     </ application >

  代碼說明:

注意,這只是代碼片段,加入項目做相應的調整。 

a)、android.permission.RECEIVE_BOOT_COMPLETED權限非必須,可以把這個和ParseBoradcastReceiver的BOOT_COMPLETED一起去掉,但重啟后可能就無法接收推送了,需要打開一次應該才可以。(我就想這樣!能少用一個權限就少一個)注意消息不會丟失,會在下一次一起收到。

b)、PushService必須要注冊,否則無法使用 

c)、ReceiverPush后面要用到,主要用於接收廣播,方便自己處理推送數據。 

 

Application

    @Override
     public  void onCreate() {
         super.onCreate();

        Parse.initialize( this, "Application ID",
                "Client Key");
         //  PushService.subscribe(this, "", WebActivity.class);
        PushService.setDefaultPushCallback( this, WebActivity. class);
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }

 代碼說明:

除了配置AndroidManifest.xml,僅在Application的onCreate中加入這三行代碼即可。

a)、注意最后一行好像是最近更新加上的,否則無法接收到Push,大意是登記注冊的意思,可以在Data Browser中對象Installation中看到。

b)、setDefaultPushCallback第二個參數,表示點擊Notifacation時處理的Activity

 

3.2 以通知(Notification)的形式顯示推送信息(狀態欄顯示通知)

進入Parse后台的Push Notifications,點擊Send a push 

3.2.1 以消息(Message)的形式發送

通過查看發送報告,發現其實也是以JSON數據發送的,不過只包含alert節點。 

 

3.2.2 以JSON形式發送 

 

title和alert分別對應Android Notification對象的標題和消息,不設置title就默認顯示APP的名稱。

最后點擊Send Notification就可以了,順利的話可以看到設備上收到Notification。

 

3.2.3 處理通知信息

當點擊狀態欄的通知時,會默認跳轉到setDefaultPushCallback指定的Activity中,可以從Intent中解析Push的數據:

 

直接從getIntent().getStringExtra("com.parse.Data")即可取到上面的信息,然后完成業務邏輯即可。

 

3.2 自定義以廣播的形式后台接受推送信息(狀態欄不顯示通知)

只要以JSON格式發送,並且不包含title和alert節點,即不會顯示Notification了(大家可以反編譯看一下StandardPushCallback類),那么如何接受Push的數據呢?

3.2.1 首先注冊Boradcast,設置Intent-filter,這里設置的action是com.nmbb.lol.push,代碼上面已經給出。

3.2.2 json數據:

{"action":"com.nmbb.lol.push","url":"http://v.youku.com/player/getRealM3U8/vid/XNTU1NjkzMDAw/type/mp4/v.m3u8"}

必須包含action和匹配的值,才能接受到推送廣播。接下來就可以做你想做的事情了!這里貼一下Boradcast代碼:

public  class ReceiverPush  extends BroadcastReceiver {

    @Override
     public  void onReceive(Context context, Intent intent) {
         if (context !=  null && intent !=  null
                && "com.nmbb.lol.push".equals(intent.getAction())) {
             try {
                JSONObject json =  new JSONObject(intent.getExtras().getString(
                        "com.parse.Data"));
                String title = json.optString("title");
                String message = json.optString("message");
                String action = json.optString("action");
                String url = json.optString("url");
                
                ToastUtils.showLongToast(message);
                
            }  catch (JSONException e) {
                Logger.e(e);
            }
        }
    }

}

 

3.3 其他注意

a). 發送消息時注意右上角的recipients的數量,表示收到推送用戶的數量。

b). 注意在Settings中開啟Client push

c). 今天還碰到一個特別奇怪的問題,死活收不到推送,不知道是不是和APP名稱設置為中文有關系,刪了重建弄個英文的又好了。 

 

四、文章

Android Push Notifications In Parse: A deep overview

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM