Jpush使用總結


今天對極光推送(Jpush )的使用方法做了個測試,首先在其官方網站上下載Android的jar包和demo,(也可以在注冊並且輸入包名的情況下下載demo,此時的demo就是一個已經配置好包名和APIKey的demo,可以直接將工程部署到手機上進行測試。)。

極光使用了兩種不同的通知方式,一種是推送通知,一種是推送消息。
    1.推送通知,此時發送的內容只是一條簡單的文本消息。

點擊消息后,會跳轉到一個Activity(此Activity需要在清單文件中注冊過濾器  
  <activity android:name = "com.example.jpushdemo.TestActivity" >
            <intent-filter>
                <action android:name="jpush.testAction" />
                <category android:name="jpush.testCategory" />
            </intent-filter>
         </activity>
  )中,可以在此Activity中以獲取傳遞Intent的方式獲取到message的對象。並將其顯示在Activity中。
    tv_title = (TextView) findViewById(R.id.tv_title);
        tv_content = (TextView) findViewById(R.id.content);
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            tv_title.setText(title);
            tv_content.setText(content);
        }
一般來說如果不需要太多復雜內容的話,可以直接使用此種方式,將數據封裝成一個Json,將其推送,客戶端收到后將json解析出來並展現到前台即可。
此種方式有缺陷 ,極光中對推送通知中文本的大小有限制,為72個漢字(或144個其它類型字符)。傳輸信息有限。
 
    2.推送消息,根據Demo可以看到,自定義消息接收需要客戶端接收一個消息廣播接收者。
 
   <!-- User defined.  For test only  用戶自定義的廣播接收器-->
        <receiver
            android:name="com.example.jpushdemo.MyReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用戶注冊SDK的intent-->
                <action android:name="cn.jpush.android.intent.UNREGISTRATION" />  
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required  用戶接收SDK消息的intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required  用戶接收SDK通知欄信息的intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required  用戶打開自定義通知欄的intent-->
                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!--Optional 用戶接受Rich Push Javascript 回調函數的intent-->
                <category android:name="org.linwoain.test" />
            </intent-filter>
        </receiver>
 
在MyReceiver中有提示
/**
 * 自定義接收器
 * 如果不定義這個 Receiver,則:
 * 1) 默認用戶會打開主界面
 * 2) 接收不到自定義消息
 */
public class MyReceiver extends BroadcastReceiver
 
消息只能在此Receiver中獲取,不會顯示成通知,或者其他。在 onReceive(Context context, Intent intent)中獲取方式為
 
 if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                  Bundle bundle = intent.getExtras();  
            Log.d(TAG"[MyReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
}
在此處也可獲取推送通知中的內容
 else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG"[MyReceiver] 接收到推送下來的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG"[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);
  
                Log. i ( TAG , bundle.getString(JPushInterface. EXTRA_ALERT ));//此處獲取到的文本就是通知的內容
 
此種方式明顯比推送通知的方式好一點的是其發送的信息容量比較大,為 236個漢字。
 
除了在網頁中控制台可以推送消息外,也可以在第三方平台上發出推送。
 
是源代碼公開的。可以直接下載jar包  https://github.com/jpush/jpush-api-java-client/releases
此jar包運行依賴於另外兩個包
  • gson
  • slf4j
將此三個包加入到新建的java工程中。同時在main函數寫入:
    //這是在極光網站上申請的密鑰
        String masterSecret="*****7a513c1406ee45658f";
        //應用的appKey,同樣在網站上申請
        String appKey="*****50726fb7d65b4c07eda";
        //建立JpushClient類,用來發送消息的對象
        JPushClient client=new JPushClient(masterSecret, appKey);
        //創建自定義消息的參數
        CustomMessageParams params = new CustomMessageParams();
        //設置接收類型
        params.setReceiverType(ReceiverTypeEnum.APP_KEY);
        params.setReceiverValue("haha");
        //可以發送通知
               //        MessageResult msgResult =client.sendNotificationAll("這里是測試發送的內容!+++1");
        //也可以發送自定義消息
        MessageResult msgResult=client.sendCustomMessage("這是一條測試的標題""這里是內容部分123", params, null);
         
 
 
如果無意外的話,即可發送一條消息到手機上。 注意:自定義消息需要在客戶端的Receiver中自行定義。不會發出提醒。
 
 
 
2014年5月4日14:28:23
 
 
附加內容,自定義通知欄的樣式的兩種方法:
    
    1、發送自定義消息,自己定義Notification的樣式並顯示。
    2、發送推送消息。需要用到Jpush包提供的API。
    當用戶需要定制默認的通知欄樣式時,則可調用此方法。

極光 Push SDK 提供了 2 個用於定制通知欄樣式的構建類:

  • BasicPushNotificationBuilder
    • Basic 用於定制 Android Notification 里的 defaults / flags / icon 等基礎樣式(行為)
  • CustomPushNotificationBuilder
一般使用BasicPushNotificationBuilder即可。
    BasicPushNotificationBuilder builder =  new  BasicPushNotificationBuilder(
        getApplicationContext());
    builder.notificationDefaults = Notification.DEFAULT_LIGHTS;  //只設置了默認閃光燈
    builder.notificationFlags = Notification.FLAG_AUTO_CANCEL// 設置為自動消失
    JPushInterface.setPushNotificationBuilder(1, builder);  //設置 Jpush 的通知欄樣式為build,並設置樣式編號為1,發送推送時需使用此編號,此方法需在 Jpush 初始化后使用  
    
    因為在業務中需要自定義聲音又要求使用推送方式。所以設置一個通知欄樣式中聲音不設置。在收到推送的事件(Receiver)中播放一段聲音。使用SoundPool方式,在應用初始化時初始化一個SoundPoll對象和一個int值
     public static SoundPool sound;
    public static int soundId;
 
初始化對象
        sound = new SoundPool(3,  AudioManager.STREAM_MUSIC, 0);
        soundId = sound.load(getApplicationContext(), R.raw.lingsheng, 1);
 
在Receiver中播放
    if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
        .getAction())) {
        sound.play(MyApp.soundId, 1.0f, 1.0f, 1, 0, 1.0f);//利用初始化后的SoundPool對象播放
    }
 
 
 
 
 
 
 
 
 
 
 






免責聲明!

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



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