preface
At present, Android applications cannot receive notifications after killing the process in the background. In order to receive push messages, they need to integrate push SDKS of various manufacturers. Take Xiaomi as an example
1. Prepare for access
1. Log in to the Xiaomi developer website (dev.mi.com/console/app…) , create an App and get the AppId, AppKey and AppSecret.
2. Download the Android client SDK package.
Download address: dev.xiaomi.com/mipush/down… .
The compressed package contains Server SDK, Client SDK, and Android DEMO.
2. SDK access guide
Configure the AndroidManifest.xml file
The xiaomi Push SDK supports the lowest Version of Android 2.3.
<uses-sdk android:minSdkVersion="9"/>
Copy the code
List of permissions required by the push service:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.VIBRATE"/> <permission android:name="com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" android:protectionLevel="signature" /> <! Com - here. Xiaomi. Mipushdemo to app package name - > < USES - permission android: name = "com. Xiaomi. Mipushdemo. Permission. MIPUSH_RECEIVE" / > <! Com.xiaomi. Mipushdemo -->Copy the code
The following services and receivers need to be configured:
<service android:name="com.xiaomi.push.service.XMPushService" android:enabled="true" android:process=":pushservice" /> <! - note: This service must be after 3.0.1 version (including 3.0.1 version) to join - > < service android: name = "com. Xiaomi. Push. Service. XMJobService" android: enabled = "true" android:exported="false" android:permission="android.permission.BIND_JOB_SERVICE" android:process=":pushservice" /> <service android:name="com.xiaomi.mipush.sdk.PushMessageHandler" android:enabled="true" android:exported="true" /> <! - note: This service must be after 2.2.5 version (including 2.2.5 version) to join - > < service android: name = "com. Xiaomi. Mipush. SDK. MessageHandleService" android:enabled="true" /> <receiver android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver" android:exported="true"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <receiver android:name="com.xiaomi.push.service.receivers.PingReceiver" android:exported="false" android:process=":pushservice"> <intent-filter> <action android:name="com.xiaomi.push.PING_TIMER" /> </intent-filter> </receiver>Copy the code
XMPushService and PingReceiver are defined in the PushService process, which you can also configure to run in any process. If the Android :process property is not configured, they will run in the main process of the application.
2.2. Customize a BroadcastReceiver class
To receive messages, you need to define a custom BroadcastReceiver that inherits from the PushMessageReceiver class
public class DemoMessageReceiver extends PushMessageReceiver { private String mRegId; private long mResultCode = -1; private String mReason; private String mCommand; private String mMessage; private String mTopic; private String mAlias; private String mUserAccount; private String mStartTime; private String mEndTime; @Override public void onReceivePassThroughMessage(Context context, MiPushMessage message) { mMessage = message.getContent(); if(! TextUtils.isEmpty(message.getTopic())) { mTopic=message.getTopic(); } else if(! TextUtils.isEmpty(message.getAlias())) { mAlias=message.getAlias(); } else if(! TextUtils.isEmpty(message.getUserAccount())) { mUserAccount=message.getUserAccount(); } } @Override public void onNotificationMessageClicked(Context context, MiPushMessage message) { mMessage = message.getContent(); if(! TextUtils.isEmpty(message.getTopic())) { mTopic=message.getTopic(); } else if(! TextUtils.isEmpty(message.getAlias())) { mAlias=message.getAlias(); } else if(! TextUtils.isEmpty(message.getUserAccount())) { mUserAccount=message.getUserAccount(); } } @Override public void onNotificationMessageArrived(Context context, MiPushMessage message) { mMessage = message.getContent(); if(! TextUtils.isEmpty(message.getTopic())) { mTopic=message.getTopic(); } else if(! TextUtils.isEmpty(message.getAlias())) { mAlias=message.getAlias(); } else if(! TextUtils.isEmpty(message.getUserAccount())) { mUserAccount=message.getUserAccount(); } } @Override public void onCommandResult(Context context, MiPushCommandMessage message) { String command = message.getCommand(); List<String> arguments = message.getCommandArguments(); String cmdArg1 = ((arguments ! = null && arguments.size() > 0) ? arguments.get(0) : null); String cmdArg2 = ((arguments ! = null && arguments.size() > 1) ? arguments.get(1) : null); if (MiPushClient.COMMAND_REGISTER.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mRegId = cmdArg1; } } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mAlias = cmdArg1; } } else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mAlias = cmdArg1; } } else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mTopic = cmdArg1; } } else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mTopic = cmdArg1; } } else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mStartTime = cmdArg1; mEndTime = cmdArg2; } } } @Override public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) { String command = message.getCommand(); List<String> arguments = message.getCommandArguments(); String cmdArg1 = ((arguments ! = null && arguments.size() > 0) ? arguments.get(0) : null); String cmdArg2 = ((arguments ! = null && arguments.size() > 1) ? arguments.get(1) : null); if (MiPushClient.COMMAND_REGISTER.equals(command)) { if (message.getResultCode() == ErrorCode.SUCCESS) { mRegId = cmdArg1; }}}}Copy the code
Register the custom BroadcastReceiver in the AndroidManifest.xml file
<receiver android:exported="true" android:name="com.xiaomi.mipushdemo.DemoMessageReceiver"> <! -- here com. Xiaomi. Mipushdemo. DemoMessageRreceiver into the full name of the class defined in the app - > < intent - filter > < action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" /> </intent-filter> </receiver>Copy the code
2.3. Register for push service
By calling the MiPushClient. RegisterPush to initialize the millet notification service. After successful registration, you can receive the registration result in the custom onCommandResult and onReceiveRegisterResult, where regId is the unique indication of the current app on the current device. You can upload regId to your own server for sending messages to it.
To increase push registration, you can initialize push in the onCreate of the Application. You can also initialize push elsewhere as needed. The code is as follows:
public class DemoApplication extends Application { public static final String APP_ID = "your appid"; public static final String APP_KEY = "your appkey"; public static final String TAG = "your packagename"; @Override public void onCreate() { super.onCreate(); / / initialize the push notification service if (shouldInit ()) {MiPushClient. RegisterPush (this APP_ID, APP_KEY); NewLogger = new LoggerInterface() {@override public void setTag(String tag) {// ignore} @Override public void log(String content, Throwable t) { Log.d(TAG, content, t); } @Override public void log(String content) { Log.d(TAG, content); }}; Logger.setLogger(this, newLogger); } private boolean shouldInit() { ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)); List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses(); String mainProcessName = getApplicationInfo().processName; int myPid = Process.myPid(); for (RunningAppProcessInfo info : processInfos) { if (info.pid == myPid && mainProcessName.equals(info.processName)) { return true; } } return false; }}Copy the code
3. The test
Create a push
Fill in push content
If you close the app in the background and click push, you can still receive a push notification, indicating that the integration is successful.