1. 程式人生 > >極光推送JPush---自定義提示音

極光推送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 還是會有。開發者可以取到通知內容外的其他資訊。

終於搞定了,再也不想用極光,好麻煩-。-

相關推薦

極光JPush---定義提示

極光推送提供三種方法實現Notification通知 三方開發平臺傳送普通訊息,客戶端設定PushNotificationBuilder,實現基礎的Notification通知 三方開放平臺傳送普通訊息,客戶端設定CustomPushNotification

iOS 友盟播放定義提示(小白必讀)

越來越多的APP開始使用自己的推送提示音,下面咱就來試試。侷限型:說到推送提示音,挺多的人都會想,在收到推送的時候播放一個音訊檔案不就OK啦。這種方法有個弊端,而且可以分為兩種情況1.APP處於前臺活躍狀態:系統推送預設的提示音和自己播放的音訊檔案都會響起2.APP處於後臺或

Jpush極光定義聲音(服務端寫法)

1.JpushClientUtil package com.liuhuan.service; import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIR

iOS 極光接收定義訊息

//新增監聽者     NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];     [defaultCenter

第一篇部落格:極光以及定義聲音

極光註冊就不說了,首先配置App的build.grandle 在defaultConfig裡貼上一下內容appkey為我在極光註冊之後的Appkey 然後配置AndroidManifest  把下面這些資訊放在application裡面 <!--極光的-->

通知定義提示聲音

在自定義推送通知聲音的時候,推送如果不是使用自定義訊息,而是應用平臺預設的通知的話,一般可能沒有辦法設定自定義的聲音。解決辦法。 為了修改自定義聲音可以使用自定義訊息,但是為了不增加額外的工作量,可以在receiver接收到通知的時候自定義播放一段鈴聲。 播放鈴聲的程式碼片

iOS-定義提示

無論是信鴿推送,還是極光推送,其實就是將蘋果原生的APS進行封裝。具體操作步驟在相應的官網上都有詳細介紹以及Demo。本文主要講述自定義聲音遇到的坑,以下是相關硬性條件: 1.音訊檔案格式: .aiff 、.wav 、.caf  ,下面的/System/Librar

關於極光Jpush的demo

安卓 lips 介紹 gis 表示 request ice 通知欄 efault 關於極光推送Jpush   推送是手機app必不可少的一樣功能,這次由於公司項目需要研究了一下。由於推送一般寫於服務端,所以對於不會Android的javaweb程序員要寫出一個完整的demo

ionic 之cordova 極光jpush和百度定位外掛結合使用之定位失效問題解決辦法

之前專案已經裝了cordova的百度定位外掛,曾經出現過定位失敗問題,那是由於專案上傳svn再checkout下來的話會丟失.so檔案。只需把.so檔案補齊就OK了。但是最近由於專案需要推送訊息功能,查了下,使用了cordova集合的極光推送jpush外掛,add了以後,就出現了我百思不得其解的bu

iOS播放定義提示和震動

@implementation LKVibrate{ SystemSoundID soundID; } void soundCompleteCallback(SystemSoundID so

React Native 整合極光 JPush出現的一些么蛾子

Xcode報錯 error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can’t locate fi

iOS 本地以及定義聲音

iOS10.0以後蘋果要求本地推送使用UserNotification框架來做本地推送, 下文就該框架下做推送以及自定義推送聲音做下介紹 1.AppDelegate.m: 1.匯入框架並遵循協議: #import <UserNotificat

Android開發之魅族手機收不到極光JPush通知

情景 收不到通知,狀態列沒有、聲音沒有、控制檯不列印log 如果是控制檯不列印log,那就找MyReceiver找到推送的程式碼部分,檢視編譯是否有問題,有問題的話是肯定不通過的。而且日誌也顯示不出來。 正常的收到推送的話會出來兩條訊息,一條是系統通知,一

極光Jpush配置步驟

1.解壓sdk檔案 2.在在官網登陸建立應用並獲取appkey和packagename 3.開啟AS建立應用,包名一定要設定為packagename 4.將解壓的sdk檔案中libs目錄下的所有檔案複製在專案中libs目錄下,並通過程式碼程式碼生成jnilibs目錄。and

Swift使用極光JPush的Demo

JPushDemo github專案地址 需要匯入JPush框架,可以使用cocoapod匯入,也可以手動匯入 環境配置 配置環境可以參考極光推送的官方文件: 使用說明 待環境配置好了之後,就可以進入極光推送開始推送訊息了 推送

極光JPush java服務端程式碼

public void sendNotificationWirhAlias_Android(String title,String notification,String alias){ try { PushPayload payload = PushPayload

極光JPush客戶端與伺服器端的整合

一、應用場景     有些情況下,我們專案的伺服器端需要“自己”擁有推送訊息到手機客戶端的能力,比如,一些商家端的APP應用,當用戶下單後,我們的後臺系統需要即時的通知到商家APP端,這就要求我們的後臺系統擁有推送功能。最經濟便捷的一種方式就是伺服器端整合一套第三方的推

Android Studio整合極光(Jpush) 報錯 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco

Android studio 整合極光推送(Jpush) (華為手機)報錯, E/JPush: [JPushGlobal] Get sdk version fail![獲取sdk版本失敗!] W/System.err: java.lang.UnsatisfiedLinkError: cn.jpush.a

極光JPush之送達率低可能的原因解析

簡介 極光推送JPush訊息,相信大家各位開發者都會或多或少的使用到,網上一大片JPush訊息推送快,送達率高等等等…….. 接下來開始接入JPush推送的SDK,經過推送測試,好,發現速度還是蠻快的,多那幾個手機測試,同時發了下,發現一切都ok了,好像並沒

Android快速整合極光,內含定義通知,通知物件到某一個人,或者某一群人

整合極光推送 使用jcenter 自動整合步驟 說明 : 使用 jcenter 自動整合,不需要在專案中新增 jar 和 so,jcenter 會自動完成依賴;在 AndroidManifest.xml 中不需要新增任何 JPush SDK 相關的配置,jcen