這個週末,是最頹廢的一個週末了。工作以來,基本每個週末都會學習點知識或者總結些東西。這個週末,身體不是很舒服,結果躺牀上打了兩天農藥,也沒有寫博客。今天週一,正好我工作不忙,把上週末的博客補上。今天的博客,源於今天項目中的一個小需求:推送。這個需求不是我來做,但是我也有點興趣,就研究了一下極光推送,並嘗試着做了集成。好了,接下來總結一下極光推送在Android的集成和使用。
一.註冊和創建應用
1.使用郵箱和手機號註冊極光推送,註冊完後登錄,創建應用。在下方推送設置中點擊去設置,配置應用的包名。
2.保存包名後,下載Demo,可以參考裏面的一些代碼。
二.配置極光推送
極光推送有手動配置和Jcenter自動配置兩種方式。在這裏,我使用的是Jcenter自動配置。
1.module的build.gradle文件中添加依賴:
(1)配置applicationId
applicationId "com.xxxxxxxxxx” //Jpush包名
(2)配置.so庫
ndk {
//選擇要添加的對應 cpu 類型的 .so 庫。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 還可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
(3)配置manifestPlaceholders
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "xxxxxxxxxxxx”, //JPush 上註冊的包名對應的 Appkey.
JPUSH_CHANNEL : "developer-default", //暫時填寫默認值即可.
]
(4)配置JPush和JCore
implementation 'cn.jiguang.sdk:jpush:3.3.4' // 此處以JPush 3.3.4 版本爲例。
implementation 'cn.jiguang.sdk:jcore:2.1.2' // 此處以JCore 2.1.2 版本爲例。
2.配置自定義Service
(1)繼承JCommonService創建一個Service
package com.example.tuduzhao.tudujpush;
import cn.jpush.android.service.JCommonService;
/**
* Created by Tudu.Zhao
* on 2019/9/23
**/
public class TuDuPushService extends JCommonService {
}
(2)AndroidManifest中配置
<service android:name=".TuDuPushService"
android:enabled="true"
android:exported="false"
android:process=":pushcore">
<intent-filter>
<action android:name="cn.jiguang.user.service.action" />
</intent-filter>
</service>
3.配置自定義廣播
(1)繼承JPushMessageReceiver創建Receiver,重寫其中用到的幾個方法。
package com.example.tuduzhao.tudujpush;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;
/**
* Created by Tudu.Zhao
* on 2019/9/23
**/
public class TuDuPushReceiver extends JPushMessageReceiver {
@Override
public Notification getNotification(Context context, NotificationMessage notificationMessage) {
return super.getNotification(context, notificationMessage);
}
@Override
public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
Log.d("TTTT","Opened");
MainActivity.startActivity(context,notificationMessage.notificationContent);
super.onNotifyMessageOpened(context, notificationMessage);
}
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
Log.d("TTTT","Arrived");
super.onNotifyMessageArrived(context, notificationMessage);
}
@Override
public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
Log.d("TTTT","Dismiss");
super.onNotifyMessageDismiss(context, notificationMessage);
}
}
(2)AndroidManifest中配置receiver
<receiver
android:name=".TuDuPushReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<category android:name="com.example.tuduzhao.tudujpush" />
</intent-filter>
</receiver>
三.初始化極光推送
1.新建Application,在onCreate中初始化
package com.example.tuduzhao.tudujpush;
import android.app.Application;
import cn.jpush.android.api.JPushInterface;
/**
* Created by Tudu.Zhao
* on 2019/9/23
**/
public class TuDuApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}
2.在Manifest中配置自定義的Application
android:name=".TuDuApplication"
四.測試
經過上面的步驟,我們已經配置好了極光推送,接下來,測試一下我們的推送。在點擊通知的時候,打開主頁,同時展示我們接收到的通知信息。
1.主頁Activity
package com.example.tuduzhao.tudujpush;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
public static void startActivity(Context context,String message){
Intent intent = new Intent();
intent.setClass(context,MainActivity.class);
intent.putExtra("message",message);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
Intent intent = getIntent();
if(intent!=null){
String message = intent.getStringExtra("message");
tv.setText(message);
}
}
}
2.發送推送消息
2.查看結果:
以上,就是對極光推送集成和基本使用的總結。通過本篇文章,我們可以發現,集成極光推送可以輕鬆實現app的推送,隨時隨地的推送。