Aurora Push Usage Example (ii) Android client

Source: Internet
Author: User

The previous article briefly introduced the implementation of Aurora push in Java server, if interested can look at the Aurora push use instance (a) Java service side. This article describes the implementation of the Aurora push in the Android client.
The Jpush Android SDK is the ability to run long in the background as Android Serivice, thereby creating and maintaining long connections, keeping it always online. Jpush Android SDK Due to the use of custom protocols, the protocol body is extremely small, the traffic consumption is very small. On the battery level, the Jpush Android SDK is continuously optimized to minimize unnecessary code execution, and the long-term version upgrade iterations are constantly tuned to minimize the need for a certain network connection stability and reduce power consumption. The complexity and instability of the Android device network is one of the most complex areas of Android device development.
Key points:
1. We strongly recommend that you download the Android demo on the Aurora official website, with the necessary jar files and configuration files. Configure the correct package name to get the Appkey value of the project
2. Aurora push is implemented via broadcast, so the key to Android client is to register the broadcast event
3. It is recommended to initialize the Jdpush when the project is initialized application (initialize before you can push the service)
4. Here we implement all user push and individual user push

Download the website demo and deploy, we will see the following effect

See the specific code below, click Initpush is the initialization of Jdpush. It is recommended that this step be implemented during application initialization in a specific project. The initialization code is actually very simple.

// 初始化 JPush。如果已经初始化,但没有登录成功,则执行重新登录。    private void init(){         JPushInterface.init(getApplicationContext());    }

Broadcast Messagereceiver, first determine whether the broadcast is the current broadcast based on Message_received_action and then get the pushed content from the broadcast

    PublicClassmessagereceiver extends broadcastreceiver { @Override public void OnReceive (Context Context, Intent Intent) { if (message_received_action.equals (Intent.getaction ())) {String Messge = Intent.getstringextra (Key_message); String Extras = Intent.getstringextra (Key_extras); StringBuilder showmsg = new StringBuilder () showmsg.append (Key_message + ":" + messge + "\ n"); if (! Exampleutil.isempty (Extras)) {showmsg.append (Key_extras + ":" + extras + "\ n");} Setcostommsg (Showmsg.tostring ()); } } }

Method of registering broadcast registermessagereceiver, mainly Registerreceiver (Mmessagereceiver, filter) method

public void registerMessageReceiver() {        mMessageReceiver = new MessageReceiver();        IntentFilter filter = new IntentFilter();        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(MESSAGE_RECEIVED_ACTION); registerReceiver(mMessageReceiver, filter); }

1

    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Finally, in the OnCreate method

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        initView();           registerMessageReceiver();  // used for receive msg }

OK, as soon as we click on the init button, the log will show the success of initialization. Then push a service from the background and you'll see the log showing the following information.

12L0708:35:36.235:v/jpush (693): [Jpushinterface] Action:getpushnotificationbuilder:012L0708:35:36.275:d/phonestatusbar (276): AddNotification score=012L0708:35:36.325:d/jpush (693): [Notificationhelper] Send push received broadcastTo developer defined Receiver12L0708:35:36.335:d/jpush (693): [Myreceiver] onreceive-cn.jpush.android.intent.notification_received, Extras:12L0708:35:36.335:d/jpush (693): Key:cn.jpush.android.MSG_ID,Value39958893512L0708:35:36.335:d/jpush (693): Key:cn.jpush.android.ALERT,Value: I wish you a happy Spring festival12L0708:35:36.335:d/jpush (693): Key:cn.jpush.android.NOTIFICATION_CONTENT_TITLE,Value:jpush SDK Demo12L0708:35:36.335:d/jpush (693): Key:cn.jpush.android.EXTRA,value:{}12L0708:35:36.335:d/jpush (693): Key:cn.jpush.android.PUSH_ID,Value39958893512L0708:35:36.335:d/jpush (693): Key:cn.jpush.android.NOTIFICATION_ID,value:39958893502- :36.335:d/jpush (693): [Myreceiver] received push down notification 02- : 36.335:d/jpush (693): [Myreceiver] The ID of the notification received for push down: 39958893502-07 : 36.355:d/mediaplayer (276): Couldn' t open file on client side, trying server side
                                                                                                

You can see the icon in the upper left corner of the picture to remind us to receive the push message.

Above our backstage is all the user push content for all operation platform (including Andorid,ios), then how to push content for one user? As mentioned in the previous article, we need to set the alias.

OK, click Advanced Features, you will see the following interface, and I fill in the alias in the mobile phone number

You can see that the code for the alias is set by calling handler.

private void Setalias () {EditText Aliasedit = (EditText) Findviewbyid (R. ID. Et_alias); String alias = Aliasedit. GetText (). ToString (). Trim (); if (textutils. IsEmpty (alias)) {Toast. Maketext (pushsetactivity. this,r.string.error_alias_empty, Toast.show ()  return .isvalidtagandalias (alias) {Toast.this,r.string.error_tag_gs_empty, Toast.show ()  return .sendmessage (Mhandler .obtainmessage (Msg_set_alias, ALIAS))              

Handler is calling the following code to set the alias value.

  case MSG_SET_ALIAS:                Log.d(TAG, "Set alias in handler."); JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj, null, mAliasCallback); break;

Then we can according to set the alias value, combined with the first blog background server Setup method, based on the identified alias to push. Here is the background server's settings for alias in the previous blog post

public static PushPayload buildPushObject_all_alias_alert() {        return PushPayload.newBuilder()                .setPlatform(Platform.android())//设置接受的平台                .setAudience(Audience.alias("18810923631"))//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到 .setNotification(Notification.alert(ALERT)) .build(); }

In addition to aliases (alias), the Aurora push-back provides the tag (tag), primarily for a specific set of users to push. There are also settings to set the push time and the style of the notification bar, you can also study. The important thing is to understand the principle.

Finally, let's take a look at the schematic.

Aurora Push Usage Example (ii) Android client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.