iOS on the use of carrier pigeon push demo details

  • 2021-01-06 00:45:21
  • OfStack

Recently, when you look at push, you use carrier pigeon push mainly because you use carrier pigeons in the background

Push with the third party push, that is, in the client to build a broadcast receiver, when the server sends a message sent to the carrier pigeon, carrier pigeon again sent 1, the broadcast receiver to accept;

The function I implemented is relatively simple. When app is in running state, a pop-up window will be displayed on the home page to display the pushed messages. Show the default notification if app is not running and service is not destroyed

So how to display the pop-up on the home page: when the broadcast receiver receives the message I want, use the observer mode, receive the message in the send a message to the main screen

Official Demo connection: http: / / xg qq. com xg/help/ctr_help/download

Modified Demo address: http: / / download csdn. net/detail/qq_29774291/9635735

1. First add good permissions and services according to the example on the official website. Some services should be changed to their own package name;

2. Copy the broadcast receiver from the official website to your own project, and then add it to the list file;

3. Then added 3 AccessKey from the official website


<meta-data android:name="com.tencent.rdm.uuid" android:value="eb5fa555d70c3246a4944f55be8c266b" />
<!--  "Must"   Please send YOUR_ACCESS_ID Modified to APP the AccessId ." 21 "At the beginning of 10 Bit number, no space in between  -->
<!--  "Must"   Please amend for APP the AccessId ." 21 "At the beginning of 10 Bit number, no space in between  -->
<meta-data
android:name="XG_V2_ACCESS_ID"
android:value="2100219302" />
<!--  "Must"   Please amend for APP the AccessKey ." A "At the beginning of 12 Bit string with no space between  -->
<meta-data
android:name="XG_V2_ACCESS_KEY"
android:value="A15KJ71W9ELC" />

4. Now the main purpose of registering carrier pigeon push in the main interface is to get an token, which can be uploaded to the server. Now the server can send messages to you; Of course you can send messages from the official background of the homing pigeon, but sometimes messages sent from the background of the homing pigeon will not be accepted, which is not as good as the Aurora push

XGPushConfig.enableDebug(this, true); When Posting this, change it to false or delete it


// A homing pigeon start
private String token;
private Message message = null;
private void XGInit() {
// TODO Auto-generated method stub
XGPushConfig.enableDebug(this, true);
//  If you need to know if the registration was successful, use the registerPush(getApplicationContext(),
// XGIOperateCallback) with callback version 
//  If you need to bind an account, use registerPush(getApplicationContext(),account) version 
//  Refer to the detailed development guide for details 
//  The argument passed is ApplicationContext
//Context context = getApplicationContext();
//1. Acquisition of device Token
Handler handler = new HandlerExtension(MainActivity.this);
message = handler.obtainMessage();
XGPushManager.registerPush(getApplicationContext(), new XGIOperateCallback() {
@Override
public void onSuccess(Object data, int flag) {
// TODO Auto-generated method stub
Log.d("jiejie", "+++ register push sucess. token:" + data + " " + flag);
token = data + "";
message.obj = "+++ register push sucess. token:" + data;
System.out.println(token);
message.sendToTarget();
}
@Override
public void onFail(Object data, int errCode, String msg) {
// TODO Auto-generated method stub
Log.d("jiejie", "+++ register push fail. token:" + data
+ ", errCode:" + errCode + ",msg:"
+ msg);
message.obj = "+++ register push fail. token:" + data
+ ", errCode:" + errCode + ",msg:" + msg;
message.sendToTarget();
}
});
}
private static class HandlerExtension extends Handler{
WeakReference<MainActivity> mActivity;
HandlerExtension(MainActivity activity) {
mActivity = new WeakReference<MainActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
MainActivity theActivity = mActivity.get();
if(theActivity == null){
theActivity = new MainActivity();
}
if(msg != null){
Log.d(Constants.LogTag, msg.obj.toString());
System.out.println("ddd"+msg.obj.toString());
}
}
}

5. Modify the onTextMessage method in the broadcast receiver so that when a message is sent, it will be sent to the main screen without displaying the notification

But first you need to know if you are running app


/**
*  Determine if it is running in the foreground 
* 
* @param context
* @return
*/
public static boolean isRunningForeground(Context context) {
String packageName = getPackageName(context);
String topActivityClassName = getTopActivityName(context);
Log.d("TAG", "packageName=" + packageName + ",topActivityClassName=" + topActivityClassName);
if (packageName != null && topActivityClassName != null && topActivityClassName.startsWith(packageName)) {
Log.d("TAG", "---> isRunningForeGround");
return true;
} else {
Log.d("TAG", "---> isRunningBackGround");
return false;
}
}
//  methods 2 And through RunningAppProcessInfo Class determination (no additional permissions required) : 
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
Log.i(" The background ", appProcess.processName);
return true;
} else {
Log.i(" The front desk ", appProcess.processName);
return false;
}
}
}
return false;
}

6. Notifies EventBus to implement Observer mode to send messages in the broadcast receiver


//  Message passthrough 
@Override
public void onTextMessage(Context context, XGPushTextMessage message) {
String text = " Messages are received :" + message.toString();
EventBus.getDefault().post(text);
System.out.println(text);
//  Getting Customizations key-value
PushTextMessage pushTextMessage = new PushTextMessage();
String title = message.getTitle();
String content = message.getContent();
pushTextMessage.setTitle(title);
pushTextMessage.setContent(content); 
String customContent = message.getCustomContent();
if (customContent != null && customContent.length() != 0) {
try {
// JSONObject obj = new JSONObject(customContent);
// // key1 Configured for the front desk key
// if (!obj.isNull("key")) {
// String value = obj.getString("key");
// LogUtils.log(LogTag, "get custom value:" + value);
// }
CustomContent custom = com.alibaba.fastjson.JSONObject.parseObject(customContent, CustomContent.class);
if (custom != null) {
pushTextMessage.setCustomContent(custom);
}
// ...
} catch (Exception e) {
System.out.println(e + "d");
e.printStackTrace();
}
}
show(context, text);
Log.d("jiejie", "pushTextMessage:" + pushTextMessage);
// EventBus.getDefault().post(pushTextMessage);
try {
// APP The process of autonomously processing messages ...
boolean isForeground = AppUtil.isRunningForeground(context);
Log.d("jiejie", isForeground + "d");
if (isForeground) {
EventBus.getDefault().post(pushTextMessage);
} else {
notify(context, title, content);
}
} catch (Exception e) {
System.out.println(e + "ddd");
e.printStackTrace();
}

7. Accept the message sent by EventBus in the main interface and display a pop-up window


@Subscribe
public void onMessageReviced(final PushTextMessage pushTextMessage){
Log.d("jiejie", " good " + "  The title " +pushTextMessage.getTitle() + "  Content: " +pushTextMessage.getContent() + "CustomContent" + pushTextMessage.getCustomContent().getCmd());
if(pushTextMessage != null){
showAlertDialog(this, pushTextMessage);
}
}
private void showAlertDialog(Context context,PushTextMessage text){
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(" Push title ");
dialog.setMessage(text.getContent());
dialog.setPositiveButton(" confirm ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
AlertDialog mDialog = dialog.create();
mDialog.show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
EventBus.getDefault().unregister(this);
}

Related articles: