Android notifications in two ways

Let’s talk about two ways for Android to get data from the server and notify the user. There are many detailed explanations on the Internet for these two ways. Here I just briefly say the advantages and disadvantages, and we choose to use it according to our business needs.

  • Polling: Based on Pull. The Android terminal takes the initiative to request the server at a fixed interval to obtain relevant data and then notify it. Comparatively speaking, it consumes power, occupies resources and the message may be delayed.
  • Push: Based on Push. When the server side has data to notify Android side, more timely

demand

I used to integrate aurora (JPush) push, but now I gave it to an Intranet server, and I had to push it. Then I researched it by myself, but I didn’t finish it yet, because my requirement is to check whether push is needed once a day, so I used polling. This is push notification via polling.

Specific steps

  1. Create PollingService
public class PollingService extends Service { private static final String TAG = "PollingService"; public static final String ACTION = "com.gzgsnet.patent.common.pull.PollingService"; // Private Notification Notification; private NotificationManager manager; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); // initialize notification (); } @Override public int onStartCommand(Intent intent, int flags, int startId) { new PollingThread().start(); Flags = START_FLAG_REDELIVERY; flags = START_FLAG_REDELIVERY; return super.onStartCommand(intent, flags, startId); }Copy the code

In the onStartCommand() method, setting flag to START_FLAG_REDELIVERY will restart the Service after the App is shut down. I tested it and the Service can be restarted automatically if you clean up the App or stop the Service in Settings. Available on Android6.0 and below.

START_FLAG_REDELIVERY indicates that the service is restarted after being killed. The last returned value was START_REDELIVER_INTENT, so the intent parameter is entered

  1. Create Notification Create Notification Settings
/** * Private void initNotification() {// System notification management class, Get NotificationManager object Manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent = new Intent(this, payactivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); notification = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_patent) .setContentTitle(getResources().getString(r.sing.app_name)).setContentText(" you need to pay for the patent. ) .setContentIntent(pendingIntent) .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setPriority(Notification.PRIORITY_MAX) .setDefaults(Notification.DEFAULT_SOUND) .build(); // Send a notification to the system manager.notify(0, notification); }Copy the code
  1. Create child threads to write business logic
Class PollingThread extends Thread {@override public void run() {log. I (TAG, "start polling... ); *** logic *** initNotification(); Log. I (TAG, "push...") ); }}}Copy the code
  1. Create the polling management tool class

Write management methods in the polling management class

Public class PollingManager {/** * startPolling */ public static void startPolling(Context Context, int seconds, class <? > cls, String action){obtain the system AlarmManager service AlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent Intent = new Intent(context, CLS); intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); / / start the service start time long triggerAtTime = SystemClock. ElapsedRealtime (); / / set the polling interval time according to system AlarmManger Service and to perform the Service alarmManager. SetRepeating (alarmManager ELAPSED_REALTIME, triggerAtTime, seconds * 1000, pendingIntent); Public static void stopPolling(Context Context, Class<? > cls, String action){// Obtain AlarmManager system service AlarmManager AlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent = new Intent(context, CLS); intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Cancel the currently executing Service alarmManager.cancel(pendingIntent); }}Copy the code
  1. Configure the Service in the manifest.xml file

The PollingService can be configured in the manifest.xml file to use the Android: Process attribute to use a single thread

<! -- polling service - > < service android: name = ". Common. Pull. PollingService "android: process =" : pollingcore" android:exported="false"/>Copy the code
  1. Open the polling

Enable polling in onCreate of MainActivity

/ / open the polling service PollingManager. StartPolling (this, 10, PollingService. Class, PollingService ACTION); Log. I (TAG, "onCreate: enable polling "); @Override protected void onDestroy() { super.onDestroy(); PollingManager.stopPolling(this, PollingService.class, PollingService.ACTION); Log. I (TAG, "onDestroy: stop polling "); }Copy the code

Reference blog: Android service preservation, explained in detail, with a good English article.

Android polling scheme

After reading some articles about polling, notification, and Service preservation, I feel more and more that I really don’t know anything. I feel like A loser.