SoFunction
Updated on 2025-03-10

Android top bar timed push messages

When using Android devices, pop-up push messages are often applied. Below, I will share with you the push code I wrote before for your reference. Friends with different opinions are welcome to propose it and learn and make progress together!

Recently, many friends have searched for this. This is just a separate built-in push. Time-by-time push is associated with server We can use SDK cloud push to achieve the needs we need. Related introduction. Move 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=":push" >
<intent-filter>
<action android:name=".MY_SERVICE" />
<category android:name="" />
</intent-filter>
</service>
<receiver
android:name=""
android:permission=".INSTALL_SHORTCUT"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name=".INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
<!--end add-->

Inside. :. These symbols are very scammed. If you don't understand, you can check the field attribute description.

Push class:

/******************************** kind *************************************/
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MessageService extends Service {
//Get message threadprivate MessageThread messageThread = null;
//Click to viewprivate Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//Notification column messageprivate int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificatioManager = null;
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//initializationmessageNotification = new Notification();
 = .app_icon_ucs;
 = "알림";
 = Notification.DEFAULT_SOUND;
messageNotificatioManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//Click on the jump activitymessageIntent = new Intent(this, );
messagePendingIntent = (this,0,messageIntent,0);
//Open threadmessageThread = new MessageThread();
 = true;
();
//(, "", Toast.LENGTH_LONG).show();
();
}
/**
 * Get messages from the server side
 *
 */
class MessageThread extends Thread{
//The operation status, the next step is very usefulpublic boolean isRunning = true;
public void run() {
while(isRunning){
try {
//Rest for 10 minutes(1000);
//Get server messageString serverMessage = getServerMessage();
if(serverMessage!=null&&!"".equals(serverMessage)){
//Update notification bar(,"알림",serverMessage,messagePendingIntent);
(messageNotificationID, messageNotification);
//Every time the notification is completed, the notification ID is incremented to avoid overwriting the messagemessageNotificationID++;
}
} catch (InterruptedException e) {
();
}
}
}
}
@Override
public void onDestroy() {
// (0);
//Or, choose one of two, it is recommended to use (0), so that the process exits cleaner = false;
//();
}
/**
 * Here is the server demo, for example only
 * @return Return the message to be pushed by the server, otherwise if it is empty, it will not be pushed.
 */
public String getServerMessage(){
SimpleDateFormat sdf=new SimpleDateFormat("HHmmss");
String date=(new ());
String in = date;
if(("195500"))
{
String str = "잠시후 전쟁터 시작됩니다. 준비해주세요.";
return str;
}
else if(("212500"))
{
String str = "잠시후 보스전 시작됩니다. 준비해주세요.";
return str;
}
else
{
return "";
}
}
}

Finally, the call method:

//PushIntent intent = new Intent();
// Set Action properties(".MY_SERVICE");
// Start the ServicestartService(intent);
// startService(new Intent(, ));

Android message push knowledge supplement:

1. Introduction

The so-called message push is to send a connection from the server to the mobile terminal and transmit certain information. For example, some news clients receive one or more notifications every once in a while, which is a push message coming from the server side; for example, some commonly used IM software such as WeChat, GTalk, etc., all have server push functions.

The push method is as follows:

1) Communication between the server and the client through SMS.

On the Android platform, you can understand the server's intention by intercepting SMS messages and parsing the message content, and can achieve complete real-time operations. But the problem is that the cost of this solution is relatively high and depends on the operator.

2) Active timing acquisition of loop

This method requires the client to make a timed or periodic access to the server-side interface to obtain the latest news. Polling too slowly may cause delays in certain messages, and too fast will consume a lot of network bandwidth and battery.

3) Persistent connection

This solution can solve the performance problems caused by polling, but it will still consume the battery of the phone. We need to open a service to maintain persistent connection with the server side (Apple and Google's C2DM are the mechanism). However, for Android systems, when the system has low available resources, the system will forcefully close our services or applications, and in this case the connection will be forced to be interrupted. (The reason why Apple's push service works well is that each phone only maintains one connection to the server. In fact, C2DM works the same way. That is, all push services are completed through a proxy server. In this case, only a persistent connection to one server is required. C2DM=Cloud to Device Messaging).

In comparison, the third one is the most feasible. Write system services or boot functions for the software; or if the system resources are low, after the service is closed, you can restart the service in the onDestroy() method to achieve a persistent connection method.

C2DM is built on Android's 2.2 system and cannot be compatible with old 1.6 to 2.1 systems; and it relies on the official C2DM server provided by Google. Due to the domestic network environment, this service is often unavailable.

The XMPP protocol built on the TCP protocol can not only provide such a persistent connection function, realize duplex communication between server and client, but also do not rely on the limitations of the system version and Google server, providing a better solution.

2. XMPP protocol

XMPP is the full name Extensible Messaging and Presence Protocol, formerly known as the Jabber project, and is an open instant messaging protocol based on XML. XMPP has been exposed to netizens because it is applied by Google Talk and NetEase Bubble. The key features of XMPP are decentralized instant messaging systems and the use of XML streaming. XMPP has been standardized by the IETF International Standards Organization.

Android push notification(androidpn) is a Java open source implementation based on the XMPP protocol, which includes the complete client and server side. This server side is basically modified and implemented based on another open source project, openfire.

The AndroidPN client needs to use an open source XMPP protocol package asmack based on Java. This package is also based on another open source project smack under openfire. However, we do not need to compile it ourselves, and we can directly use the AndroidPN client. The client uses the XMPPConnection class provided in asmack to establish a persistent connection with the server, and uses this connection to register and log in to authenticate users. This connection also receives notifications sent by the server.

The AndroidPN server side is also implemented in Java language, based on openfire open source project, but its web part adopts the spring framework, which is different from openfire. The Android PN server contains two parts. One is to listen to the XMPP service on port 5222, and is responsible for communicating with the client's XMPPConnection class. Its function is to register and authenticate the user and send push notification messages. The other part is a web server, which uses a lightweight HTTP server, which is responsible for receiving users' web requests. These two methods of the server are of great significance: when the corresponding TCP port is enclosed by the firewall, it can be accessed using polling, which helps to pass through the firewall.