Android 極光推送JPush---自定義提示音


極光推送提供三種方法實現Notification通知

  1. 三方開發平台發送普通消息,客戶端設置PushNotificationBuilder,實現基礎的Notification通知
  2. 三方開放平台發送普通消息,客戶端設置CustomPushNotificationBuilder,實現高級自定義的Notification通知
  3. 三方開放平台發送自定義消息,客戶端默認不處理此類消息,客戶端定義Receiver接收自定義消息並進行處理
  4. 使用普通內容為空,將普通消息轉成自定義消息類型,進而達到所需效果

但是前三種方案無法實現自定義聲音,只有第四種方案可實現自定義Notification並進行通知

嘗試方案一:

思路:接收三方開放平台自定義消息,自定義Notification更改提示音

AndroidManifest.xml

<receiver
    android:name=".application.MyReceiver"
    android:enabled="true">
    <intent-filter >

        <!-- Required 用戶注冊SDK的intent -->
        <action android:name="cn.jpush.android.intent.REGISTRATION" />
        <!-- Required 用戶接收SDK消息的intent -->
        <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
        <!-- Required 用戶接收SDK通知欄信息的intent -->
        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
        <!-- Required 用戶打開自定義通知欄的intent -->
        <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
        <!-- Optional 用戶接受Rich Push Javascript 回調函數的intent -->
        <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />
        <!-- 接收網絡變化 連接/斷開 since 1.6.3 -->
        <action android:name="cn.jpush.android.intent.CONNECTION" />

        <category android:name="應用包名" />
    </intent-filter>
</receiver>

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
   private static final String TAG = MyReceiver.class.getSimpleName();
   private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3;   //推送通知最多顯示條數

   @Override
   public void onReceive(Context context, Intent intent) {
      Bundle bundle =intent.getExtras();
      //
      if(intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)){
         Log.i(TAG, "接收到了通知");
         String title=bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
         String content=bundle.getString(JPushInterface.EXTRA_ALERT);
         String extra=bundle.getString(JPushInterface.EXTRA_EXTRA);
         Log.i(TAG, "標題:【"+title+"】,內容:【"+content+"】,附加參數:【"+extra+"");
      }else if(intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)){
         Log.i(TAG, "接收到了消息");
         String message =bundle.getString(JPushInterface.EXTRA_MESSAGE);
         processCustomMessage(context, bundle);
         Log.i(TAG, "接收到的消息是:【"+message+"");
      }else if(intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)){
         Log.i(TAG, "用戶正在打開通知");
      }
   }

   /**
    * 實現自定義推送聲音
    * @param context
    * @param bundle
    */
   private void processCustomMessage(Context context, Bundle bundle) {
      NotificationCompat.Builder notification = new NotificationCompat.Builder(context);

      String title = bundle.getString(JPushInterface.EXTRA_TITLE);
      String msg = bundle.getString(JPushInterface.EXTRA_MESSAGE);
      String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
      Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_mdpi);

      Intent mIntent = new Intent(context,****.class);
      mIntent.putExtras(bundle);
      mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);

      notification.setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setContentText(msg)
            .setContentTitle(title.equals("") ? "title": title)
            .setSmallIcon(R.mipmap.icon_mdpi)
            .setLargeIcon(bitmap)
            .setNumber(NOTIFICATION_SHOW_SHOW_AT_MOST);

      Log.e(TAG, "processCustomMessage: extras----->" + extras);
      if (!TextUtils.isEmpty(extras)) {
         try {
            JSONObject extraJson = new JSONObject(extras);
            if (null != extraJson && extraJson.length() > 0) {
               String sound = extraJson.getString("sound");
               if("1".equals(sound)){
                  notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.default_push_sound));
               } else {
                  notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.test));
               }
            }
         } catch (JSONException e) {
            e.printStackTrace();
         }
      }

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
      notificationManager.notify(NOTIFICATION_SHOW_SHOW_AT_MOST, notification.build());  //id隨意,正好使用定義的常量做id,0除外,0為默認的Notification
   }
}
效果實現了,跟服務端溝通,發現只有開發平台上有自定義消息,服務端沒自定義消息的api接口,煩唷!!!

嘗試方案二:

思路:查看接口文檔,發現極光推送采用Receiver接收普通消息和自定義消息,通過攔截廣播,不讓極光三方庫處理默認的Notification

查看極光AndroidManifst.xml的PushReceiver配置
<!-- Required SDK核心功能 -->
<receiver
    android:name="cn.jpush.android.service.PushReceiver"
    android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
        <!-- Required  顯示通知欄 -->
        <category android:name="應用包名" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
    <!-- Optional -->
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="應用包名" />
    </intent-filter>
</receiver>
發現極光推送通過PushReceiver來代理接收派發普通消息和自定義消息。那我就利用MyReceiver,而且優先級為65535,保證最先接收到消息

在MyReceiver中普通消息過濾中添加abortBroadcast(); 吸收廣播,禁止往下傳遞廣播,運行后提示“BroadcastReceiver trying to return result during a non-ordered broadcast”,居然是無序廣播。。。 這。。。。極光你就不能給個活路么

嘗試方案三:

思路:不讓我自定義,那我就把默認的鈴聲去掉,然后在接收到普通消息廣播之后使用SoundPool播放提示音樂
init JPush之后
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
builder.notificationDefaults = Notification.DEFAULT_LIGHTS;
builder.statusBarDrawable = R.drawable.icon_mdpi;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
JPushInterface.setPushNotificationBuilder(1, builder);
notificationDefaults 是通過設置二進制位來判斷的,DEFAULT_SOUND=1

使用紅米Note4做測試,還是會有提示音。。 不想做了。。。

嘗試方案四:

思路:查看文檔時無意間發現,當普通通知內容為空,將不執行默認的Notification,使用extra傳遞Notification.title和Notification.msg,再接收到普通消息之后執行之前實現的自定義processCustomMessage方法

接受廣播

如果全部類型的廣播都接收,則需要在 AndroidManifest.xml 里添加如下的配置信息:
<receiver
    android:name="Your Receiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.REGISTRATION" />
        <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
        <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
        <action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
        <action android:name="cn.jpush.android.intent.CONNECTION" />
        <category android:name="You package Name" />
    </intent-filter>
</receiver>

Action - JPushInterface.ACTION_NOTIFICATION_RECEIVED.

字符串值 "cn.jpush.android.intent.NOTIFICATION_RECEIVED"

功能描述:
收到了通知 Push。
如果通知的內容為空,則在通知欄上不會展示通知。
但是,這個廣播 Intent 還是會有。開發者可以取到通知內容外的其他信息。
終於搞定了,再也不想用極光,好麻煩-。-

 

 

 

 

 


免責聲明!

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



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