直接寫內(nèi)容,不廢話。喜歡看的看,不喜歡的別看,也別評論。
創(chuàng)建應(yīng)用
進入極光控制臺后,點擊“創(chuàng)建應(yīng)用”按鈕,進入創(chuàng)建應(yīng)用的界面。 填上你的應(yīng)用程序的名稱以及應(yīng)用包名這二項就可以了, 最后點擊最下方的 “創(chuàng)建我的應(yīng)用”按鈕,創(chuàng)建應(yīng)用完畢。
jcenter 自動集成步驟
確認android studio的 Project 根目錄的主 gradle 中配置了jcenter支持(基本默認支持)
buildscript {repositories {jcenter()}......
}allprojets {repositories {jcenter()}
}
android {......defaultConfig {applicationId "com.xxx.xxx" //JPush上注冊的包名.......ndk {//選擇要添加的對應(yīng)cpu類型的.so庫。abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'}manifestPlaceholders = [JPUSH_PKGNAME : applicationId,JPUSH_APPKEY : "你的appkey", //JPush上注冊的包名對應(yīng)的appkey.JPUSH_CHANNEL : "developer-default", //暫時填寫默認值即可.]......}......
}dependencies {......compile 'cn.jiguang.sdk:jpush:3.1.1' // 此處以JPush 3.1.1 版本為例。compile 'cn.jiguang.sdk:jcore:1.1.9' // 此處以JCore 1.1.9 版本為例。......
}
做到這邊如果編譯出現(xiàn)問題,需要這樣做:
注 : 如果在添加以上 abiFilter 配置之后 android Studio 出現(xiàn)以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin
則在 Project 根目錄的 gradle.properties 文件中添加:
android.useDeprecatedNdk=true
注 : 使用 NDK r17 時,可能 Android Studio 會出現(xiàn)以下提示:
A problem occurred starting process ‘command ‘/Users/xxx/Library/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64/bin/mips64el-linux-android-strip”系統(tǒng)找不到指定的文件
這是因為 NDK r17 之后不再支持 mips 平臺,在 build.gradle 里增加如下配置可解決
android {defaultConfig {.....}packagingOptions { doNotStrip '*/mips/*.so' doNotStrip '*/mips64/*.so' }}
說明 :若沒有 res/drawable-xxxx/jpush_notification_icon 這個資源默認使用應(yīng)用圖標(biāo)作為通知 icon,在 5.0 以上系統(tǒng)將應(yīng)用圖標(biāo)作為 statusbar icon 可能顯示不正常,用戶可定義沒有陰影和漸變色的 icon 替換這個文件,文件名不要變。
?
?
AndroidManifest替換變量(在本地的 AndroidManifest 中定義同名的組件并配置想要的屬性,然后用 xmlns:tools 來控制本地組件覆蓋 jcenter 上的組件,一般替換receiver,記得修改category屬性值)
AndroidManifest替換變量(在本地的 AndroidManifest 中定義同名的組件并配置想要的屬性,然后用 xmlns:tools 來控制本地組件覆蓋 jcenter 上的組件,一般替換receiver,記得修改category屬性值)
<application...><!-- 替換原生極光推送接收器 --><receiverandroid:name=".jpush.MyReceiver"android:enabled="true"android:exported="false"tools:node="replace"><intent-filter><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 用戶接收SDK通知欄信息的intent --><action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用戶打開自定義通知欄的intent --><action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收網(wǎng)絡(luò)變化 連接/斷開 since 1.6.3 --><category android:name="com.xxx.xxx" /> <!--JPush上注冊的包名 --> </intent-filter></receiver></application>
調(diào)試以及使用
在Application的onCreate()初始化Sdk
@Overridepublic void onCreate() {super.onCreate();JPushInterface.setDebugMode(true);JPushInterface.init(this);}
public class MyReceiver extends BroadcastReceiver {private static final String TAG = "JIGUANG";public static String regId;@Overridepublic void onReceive(Context context, Intent intent) {try {Bundle bundle = intent.getExtras();if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);//send the Registration Id to your server...} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 接收到推送下來的自定義消息(內(nèi)容為): " + bundle.getString(JPushInterface.EXTRA_MESSAGE));// 自定義消息不是通知,默認不會被SDK展示到通知欄上,極光推送僅負責(zé)透傳給SDK。其內(nèi)容和展示形式完全由開發(fā)者自己定義。// 自定義消息主要用于應(yīng)用的內(nèi)部業(yè)務(wù)邏輯和特殊展示需求} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 接收到推送下來的通知");String extra_json = bundle.getString(JPushInterface.EXTRA_EXTRA);if (!TextUtils.isEmpty(extra_json))Log.d(TAG, "[MyReceiver] 接收到推送下來的通知附加字段" + extra_json);// 可以利用附加字段來區(qū)別Notication,指定不同的動作,extra_json是個json字符串// 通知(Notification),指在手機的通知欄(狀態(tài)欄)上會顯示的一條通知信息} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 用戶點擊打開了通知");// 在這里根據(jù) JPushInterface.EXTRA_EXTRA(附加字段) 的內(nèi)容處理代碼,// 比如打開新的Activity, 打開一個網(wǎng)頁等..} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 用戶收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));//在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼,比如打開新的Activity, 打開一個網(wǎng)頁等..} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);} else {Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());}} catch (Exception e) {e.printStackTrace();}}
}
// 一般登錄之后調(diào)用此方法設(shè)置別名
// sequence 用來標(biāo)識一次操作的唯一性(退出登錄時根據(jù)此參數(shù)刪除別名)
// alias 設(shè)置有效的別名
// 有效的別名組成:字母(區(qū)分大小寫)、數(shù)字、下劃線、漢字、特殊字符@!#$&*+=.|。限制:alias 命名長度限制為 40 字節(jié)。
JPushInterface.setAlias(context, int sequence, String alias);
// 退出登錄刪除別名
JPushInterface.deleteAlias(Context context,int sequence);
可通過JPushInterface.ACTION_MESSAGE_RECEIVED(收到自定義消息這個方法里去定義Notification,而不是用發(fā)送通知)
...
else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));// 收到消息 顯示通知processCustomMessage(context, bundle);
}
private void processCustomMessage(Context context, Bundle bundle) {String channelID = "1";String channelName = "channel_name";// 跳轉(zhuǎn)的ActivityIntent intent = new Intent(context, LoginActivity.class);intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);// 獲得系統(tǒng)推送的自定義消息String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);//適配安卓8.0的消息渠道if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);}NotificationCompat.Builder notification =new NotificationCompat.Builder(context, channelID);notification.setAutoCancel(true).setContentText(message).setContentTitle("我是Title").setSmallIcon(R.drawable.logo120).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent);notificationManager.notify((int)(System.currentTimeMillis()/1000), notification.build());}
這里簡單說說,有人說我的博客基本是復(fù)制別人的,但是不是這樣的。我是參考了別人的文章,但是我都是在自己完成了功能后,自己發(fā)現(xiàn)了別人沒有提到的問題,自己解決了。寫博客時我不會一句話一句話去寫,基本復(fù)制別人說的,但是我會加入自己遇到的問題,以及怎么解決的經(jīng)歷。所以我不是看了別人的文章就直接復(fù)制的,有些人別胡說。
?
?
?
?
?
?
?
與50位技術(shù)專家面對面 20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔 為你收集整理的Android 极广推送接入 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。