Android Realizes System Message Push

  • 2021-12-04 19:51:29
  • OfStack

Nowadays, many applications are connected with push function, and there are many third parties about push in the market, such as Aurora, etc., so our demand is not great, and accessing Aurora will cause great waste of resources. Let's take a look at using android service to push messages locally.

1. Register an Service


import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import java.util.Calendar;
 
/**
 * Created by 70883 on 2017/8/10.
 */
public class PushSmsService extends Service {
 private NotificationManager manager;
 private PendingIntent pi;
 private MyThread myThread;
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
   return null;
   }
 
 @Override
 public void onCreate() {
  myThread = new MyThread();
  myThread.start();
  super.onCreate();
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
 }
   @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
   private void notification() {
    //  Gets the notification manager for the system  
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(getApplicationContext(),
      MainActivity.class);
    pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    Notification notification = new Notification.Builder(getApplicationContext())
      .setAutoCancel(true)
      .setContentText(" Be busy with work , I want to eat, too ")
      .setContentIntent(pi)
      .setSmallIcon(R.mipmap.ic_icon)
      .setWhen(System.currentTimeMillis())
      .build();
    notification.defaults = Notification.DEFAULT_ALL; //  Use default settings, such as bell, vibration and flashing lights  
     notification.flags = Notification.FLAG_AUTO_CANCEL; //  However, after the user clicks on the message, the message automatically disappears in the notification bar  
    notification.flags |= Notification.FLAG_NO_CLEAR;//  Click Delete in the notification bar, and the message will not still be deleted  
    manager.notify(0, notification);
   }
 private class MyThread extends Thread{
  private Calendar c ;
  @Override
  public void run() {
   while (true){
    c = Calendar.getInstance();
    if(c.get(Calendar.HOUR_OF_DAY) == 15){
      try {
       notification();
       sleep(1000*60*60);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
    }
   }
 
  }
 }
}

2. Register in AndroidMan


<service android:name=".ui.Service.PushSmsService"></service>

3. Because I need the global application, I started it in Application


public void startService() {
  Intent intent = new Intent(this, PushSmsService.class);
  //  Startup service 
  startService(intent);
 }

4. It can also be used with the server to push messages regularly


Related articles: