Android Top Bar timed push message _android

Source: Internet
Author: User
Tags android push notification

When using an Android device, it is often applied to pop-up push messages. Here I write before the push code to share to everyone, for everyone to refer to, have different views of friends Welcome to propose, common learning progress!

Recent search to see this friend more. This is also just a separate built-in push. Always push and server associated we can use the SDK cloud push to achieve the requirements we need. Related information. Move it down!

First XML

<!--android Push service-->
<service android:name= ". Messageservice "
android:enabled=" true "
android:exported=" true "
android:label=" Pushservice "
Android:launchmode= "SingleInstance"
android:persistent= "true"
android:process= ":p ush" >
< intent-filter>
<action android:name= "Com.xxxx.action.MY_SERVICE"/> <category
" Android.intent.category.LAUNCHER "/>
</intent-filter>
</service>
<receiver
android:name= "Com.tt.xxxxx.download.GTAlarmReceiver"
android:permission= " Com.android.launcher.permission.INSTALL_SHORTCUT "
android:enabled=" true "
android:exported=" true "
>
<intent-filter>
<action android:name= "com.android.launcher.action.INSTALL_ Shortcut "/>
</intent-filter>
</receiver>
<!--end Add-->

The inside. :。 These symbols are very pit dad. If you don't understand, you can check the field attribute description.

Push class:

/******************************** class *************************************/Package com.ttad.yxcb;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.app.Service;
Import Android.content.ContentResolver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.IBinder;
Import Android.widget.Toast;
Import Java.text.SimpleDateFormat;
Import com.tt.yingxiongchibis.download.GTDownloaderActivity; public class Messageservice extends Service {//Get message thread private messagethread messagethread = null;//Click to view private Intent m
Essageintent = null;
Private pendingintent messagependingintent = null;
Notification bar message Private int messagenotificationid = 1000;
Private Notification messagenotification = null;
Private Notificationmanager Messagenotificatiomanager = null; Public IBinder Onbind (Intent Intent) {return null;} @Override public void OnCreate () {//Initialize messagenotification = new N
Otification (); Messagenotification. icon = R.drawable.app_icon_ucs;
Messagenotification.tickertext = "알림";
Messagenotification.defaults = Notification.default_sound;
Messagenotificatiomanager = (Notificationmanager) getsystemservice (Context.notification_service);
Click jump Activity messageintent = new Intent (this, gtdownloaderactivity.class);
Messagependingintent = pendingintent.getactivity (this,0,messageintent,0);
Open Thread Messagethread = new Messagethread ();
Messagethread.isrunning = true;
Messagethread.start ();
Toast.maketext (Messageservice.this, "", Toast.length_long). Show ();
Super.oncreate (); /** * * */class Messagethread extends thread{//Run state, next step with large public boolean isrunning = true; public void run () {while (isrunning) {try {//rest 10 minutes thread.sleep (1000);//Get Server message String ServerMessage = Getservermessage (); if (serverme Ssage!=null&&! "". Equals (ServerMessage)) {//Update notification bar Messagenotification.setlatesteventinfo (messageservice.this, "알림", ServerMessage,
Messagependingintent); Messagenotificatiomanager.notify(Messagenotificationid, messagenotification);
After each notification, the notification ID increment, to avoid the message cover off messagenotificationid++;
The catch (Interruptedexception e) {e.printstacktrace ();}}
@Override public void OnDestroy () {//System.exit (0);//or, select one, recommend the use of System.exit (0), so that the process exits cleaner
Messagethread.isrunning = false;
Super.ondestroy (); /** * Here in this way for the server demo, only for example * @return return to the server to push the message, otherwise, if it is empty, do not push/public String getservermessage () {SimpleDateFormat sdf=n
EW SimpleDateFormat ("HHMMSS");
String Date=sdf.format (New Java.util.Date ());
String in = date; if (Date.equals ("195500")) {String str = ' 잠시후전쟁터시작됩니다.
준비해주세요. ";
return str; else if (date.equals ("212500")) {String str = ' 잠시후보스전시작됩니다.
준비해주세요. ";
return str; else {return "";}}}

Finally, call the way:

Push
Intent Intent = new Intent ();
Sets the action attribute
intent.setaction ("Com.ttad.yxcb.action.MY_SERVICE");
Start the service
StartService (intent);
StartService (New Intent (Extextactivity.this, Messageservice.class));

Android Message push knowledge Supplement:

1. Introduction

The so-called message push is from the server end to the mobile terminal to send a connection, transmission of certain information. For example, some news clients, every once in a while received one or more notices, this is from the server side of the push message, as well as some common IM software such as micro-letter, Gtalk, have a server push function.

The push method is as follows:

1 communication between server-side and client via SMS.

On the Android platform, you can intercept SMS messages and parse message content to understand the server's intentions and achieve full real-time operation. But the problem is that the cost of the scheme is relatively high and relies on operators.

2) cycle Active timing acquisition

This approach requires the client to do a regular or periodic access to the server-side interface to get the latest news. The frequency of polling is too slow, which can lead to delays in some messages, too quickly consuming network bandwidth and batteries.

3) Persistent Connection

This solution can solve the performance problems caused by polling, but it still consumes the battery on the phone. We need to open a service to maintain a persistent connection to the server (Apple and Google's C2DM is the mechanism). But for the Android system, when the system is low on available resources, the system will force the shutdown of our service or the application, in which case the connection will be forced to break. (Apple's push service works well because every phone keeps a connection to the server, and in fact C2DM works.) That is, all push services are done through a single proxy server, in which case you only need to maintain a persistent connection with a single server. C2dm=cloud to Device messaging).

By contrast, the third is the most feasible. Write system services or boot up for the software, or if the system is low on resources and the service is shut down, you can restart the service in the OnDestroy () method to achieve a durable connection.

C2DM is built into Android's 2.2 system and is not compatible with old 1.6 to 2.1 systems, and relies on Google's official C2DM servers, which are often unavailable due to the domestic network environment.

Based on the TCP protocol, the XMPP protocol can not only provide the function of the persistent connection, but also can realize the duplex communication between the server and the client, and can not rely on the system version and the Google Server, which provides a better solution.

2. XMPP Agreement

XMPP full name extensible messaging and Presence Protocol, formerly known as the Jabber Project, is an open Instant Messaging protocol based on XML. XMPP because by Google Talk and NetEase bubble application by the vast number of netizens contact. The key features of XMPP are distributed instant messaging systems and the use of XML streaming. XMPP is currently being standardized by IETF International standards Organization.

The Android Push notification (ANDROIDPN) is a Java open source implementation based on the XMPP protocol, which includes the full client and server side. The server side is basically in another open source project OpenFire based on the implementation of the modification.

ANDROIDPN client needs to use a Java based open source XMPP protocol package Asmack, which is also based on the openfire of another open source project smack, but we do not need to compile, You can use the Asmack.jar in the ANDROIDPN client directly. The client uses the Xmppconnection class provided in Asmack to establish a persistent connection with the server and through the connection for user registration and login authentication, as well as a notification sent by the receiving server through this connection.

The ANDROIDPN server side is also implemented in the Java language, based on OpenFire Open source Engineering, but its Web Part uses the spring framework, which is different from OpenFire. The ANDROIDPN server contains two parts, one is listening to the XMPP service on the 5222 port, is responsible for communicating with the client Xmppconnection class, the function is the user registers and the authentication, and sends the push notification message. The other part is a Web server that takes on a lightweight HTTP server and is responsible for receiving Web requests from users. The two ways of the server are significant: when the corresponding TCP port is closed by a firewall, it can be accessed using polling, and thus helps to pass through the firewall.

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.