Currently only Android notifications are implemented. Notification is a notification icon displayed in the status bar at the top of the mobile phone when the app is not running in the foreground and the user is expected to receive some important information. Relevant contents can be seen after the drop-down bar.
Basic usage
Android uses NotificationManager for notification management, which is obtained using the getSystemService () method of the Context, which receives a string to determine which service to obtain from the system. NOTIFICATION_SERVICE (context. NOTIFICATION_SERVICE)
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Copy the code
Then use Builder to create Notification object, but due to android version problem, need to do compatibility, here use the NotificationCompat class in support-V4 library
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("this is e title") / / title
.setContentText("this is content text") / / content
.setWhen(System.currentTimeMillis()) // The time of the prompt
.setSmallIcon(R.mipmap.ic_launcher) // Set the small graph
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) // Set the larger image
.setContentIntent(pi)
.setAutoCancel(true) // Cancel the current prompt after opening the prompt
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) // Set the prompt sound, here is the system sound
.setDefaults(NotificationCompat.DEFAULT_ALL) // Set the LED light to blink
.setPriority(NotificationCompat.PRIORITY_MAX) // Set whether the notification is important, Max indicates the most important, will pop up the interface
.build();
manager.notify(1, notification); // The 1 here represents each notification ID. Different notifications are separated by this number
Copy the code
The setLights() method takes in three parameters: the color of the LED, the length of time the LED is on, and the length of time the LED is off, all in milliseconds.
Notification notification = new NotificationCompat.Builder(context)
...
.setLights(Color.GREEN,1000.1000)
.build();
Copy the code
SetStyle () can be used to build rich text notifications, such as showing multiple lines in a file that would be too many to hide
.setStyle(new NotificationCompat.BigTextStyle().bigText("new new newenewewnewenwenenwwenwnwenwnwnxxxxxxxxxxxxxxxxxxxxx"))
.build
Copy the code
Add an image
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.deawable.big_image)))
.build
Copy the code
SetPriority () sets the importance of the notification, PRIORITY_DEFAULT the default importance, PRIORITY_MIN the lowest importance, such as when the user drops down, PRIORITY_LOW, PRIORITY_LOW, The system will zoom in on the notification icon, or change its order, PRIORITY_HIGH, the system will zoom in on the notification, or sort ahead, PRIORITY_MAX, the highest notification, the message must be seen by the user away, and a banner will pop up.
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
Context context = this;
Intent intent = new Intent(context, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("this is e title") / / title
.setContentText("this is content text") / / content
.setWhen(System.currentTimeMillis()) // The time of the prompt
.setSmallIcon(R.mipmap.ic_launcher) // Set the small graph
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) // Set the larger image
.setContentIntent(pi)
.setAutoCancel(true) // Cancel the current prompt after opening the prompt
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) // Set the prompt sound
.setDefaults(NotificationCompat.DEFAULT_ALL) // Set the LED light to blink
.setPriority(NotificationCompat.PRIORITY_MAX) // Set whether the notification is important, Max indicates the most important, will pop up the interface
.build();
manager.notify(1, notification);
Copy the code
You can’t send notifications when the activity is destroyed, so I just put notifications in a server,
What is a server?
The service (server) is the android implementation program background running solution, suitable for those who do not need to interact with the user and also requires long-term running tasks, services do not rely on any user interface, even if the program is switched to the background or the user opened another application, the service can still maintain normal operation. But instead of running in a separate process, the service depends on the application process in which it was created. When an application process is killed, all services that depend on that process also stop running. The service doesn’t start a thread, all the code is run in the main thread, which means the service is a child thread created in the main thread,
Basic usage of threads
Take a look at threads. In Android, if you need to perform some time-consuming operations, you will not do them in the main thread. Instead, you will open a separate child thread to handle them, in case the main thread is blocked.
class TestThread extends Thread{
@Override
public void run(a)
{
// }}/ / call
new TestThread().start()
Copy the code
class TestThread implements Runnable{
@Override
public void run(a){
// }}/ / call
TestThread testThread = new TestThread();
new Thread(testThread).start();
Copy the code
new Thread(new Runnable(){
@Override
public void run(a){
//
}
}).start();
Copy the code
If you want to change the UI based on the task you are performing, you need to use asynchronous message processing:
public class MainActivity extends AppCompatActivity implements View.OnclickListener{
public static final int TEXT =1;
private TextView text;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case TEXT:
// Perform UI operations
text.setText("welcome");
break;
default:
break; }}}@Override
public void onClick(View v){
switch (v.getId()){
case R.id.change_text:
new Thread(new Runnable(){
@Override
public void run (a){
Message message = new Message();
message.what = TEXT;
handler.sendMessage(message); // Send the message, and the handler receives the message
}
}).start();
break;
default:
break; }}}Copy the code
Asynchronous message processing
Android asynchronous Message processing is made up of four parts: in the Message, the Handler, MessageQueue, stars,
1. Message
A Message is a Message that is passed before a thread and can carry a small amount of information internally for exchanging data between different threads,
2. Handler
It is used to send and process messages. Sending messages is usually handled by the Handler and sendMessage methods, and the sent messages are usually returned to the Handler and handleMessage methods
3. MessageQueue
MessageQueue is a MessageQueue, mainly used to store all messages sent through the Handler, this part of the message will always be stored in the MessageQueue, waiting to be processed, each thread has only one MessageQueue object.
4. Looper
Looper is the manager of MessageQueue in each thread. After calling Looper’s loop method, it will enter a wireless loop. Whenever a message is found in MessageQueue, it will take it out and pass it to Handler’s handlerMessage method. There is also only one Looper object per thread
Create a service class in Android. New ->Services-> service will be automatically added to androidmanifest.xml.
<application>
<service
android:name=".MyService"
android:process=":push"></service>
</application>
Copy the code
public class MyService extends Service {
@Override
public void onCreate(a){ // called when the service is created
super.onCreate();
}
// The service is executed every time it is started
@Override
public int onStartCommand(final Intent intent, int flags, int startId){
return super.onStartCommand(intent,flags,startId);
}
// called when the service is stopped
@Override
public void onDestroy(a){
super.onDestroy(); }}Copy the code
App keeps sending notifications all the time
Generally, after the APP enters the background, the service is running normally and will not be killed, and the activity is also running normally. At this time, the mobile phone can accept notifications. But when the user empties the background, both of them are killed and no notifications can be sent. Google actually has a GCM notification, which is integrated with mobile phones. In the main program, the sender sends a notification to the Google server, and then the Google server receives the sent message and forwards it to the mobile phone, and the mobile phone receives the notification. However, this requires the mobile phone to have Google function, which is not available in China, and has been dropped by the wall. Then there appeared a bunch of third-party push, such as aurora push, Huawei push, Xiaomi push, these push are similar to the above GCM notice, third-party forwarding notice. For example, the aurora push is integrated in the app, which is useless when the app is killed. However, the reason why it can still be pushed is that many apps integrate this. If other apps are not killed, the aurora integrated in other apps will send the current notification by proxy, so as to achieve the purpose of notification. While Huawei Xiaomi should have push service between mobile phones, which should be more stable. To say why wechat, QQ program can not kill the background, can always push, probably because of py transaction with the manufacturer, but like my Huawei mobile phone turn off the startup management of the application, allow background activities, allow associated startup, allow since the start, will not push. But with the adb shell ps | findstr searches tencent
The service code
package com.selectot;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.service.MyIntentService;
import com.service.WLWakefulReceiver;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
public class MyService extends Service {
private BroadcastReceiver mReceiver;
private IntentFilter mIf;
private Intent testIntent;
private Context context;
public MyService(a) {}private String TAG = "targets";
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate(a){
super.onCreate();
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId){
Log.d(TAG, "onStartCommand: "+"fas");
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run(a) { init(); }}; timer.schedule(task,0.5000);
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy(a){
Log.d(TAG, "onDestroy: "+"Service destroyed");
super.onDestroy();
}
public void sendNotice(a){
Context context = this;
Intent intent = new Intent(context, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("this is e title") / / title
.setContentText("this is content text") / / content
.setWhen(System.currentTimeMillis()) // The time of the prompt
.setSmallIcon(R.mipmap.ic_launcher) // Set the small graph
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) // Set the larger image
.setContentIntent(pi)
.setAutoCancel(true) // Cancel the current prompt after opening the prompt
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) // Set the prompt sound
.setDefaults(NotificationCompat.DEFAULT_ALL) // Set the LED light to blink
.setPriority(NotificationCompat.PRIORITY_MAX) // Set whether the notification is important, Max indicates the most important, will pop up the interface
.build();
manager.notify(1, notification);
}
private void init(a){
Log.d(TAG, "init: " + "Send");
// (Application Processor) to prevent screen darkening. After all WakeLock is released, the system will hang.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ON_AFTER_RELEASE
| PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();
this.sendNotice(); wakeLock.release(); }}Copy the code
call
// Start the service
@ReactMethod
public void startService(a) {
Context context = MainActivity.getContext();
Intent in = new Intent(context, MyService.class);
context.startService(in);
}
Copy the code
You can also write the notification method directly
@ReactMethod
public void sendNotice(ReadableMap map, Callback callback){
MainActivity context = MainActivity.mainActivity;
// This must be the mainActivity's this,
// In onCreate, not context, otherwise clicking on the event will not jump to the screen
String title = map.getString("title");
String text = map.getString("text");
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title) / / title
.setContentText(text) / / content
.setWhen(System.currentTimeMillis()) // The time of the prompt
.setSmallIcon(android.R.mipmap.sym_def_app_icon) // Set the small graph
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), android.R.mipmap.sym_def_app_icon )) // Set the larger image
.setAutoCancel(true) // Cancel the current prompt after opening the prompt
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) // Set the prompt sound
.setDefaults(NotificationCompat.DEFAULT_ALL) // Set the LED light to blink
.setContentIntent(pi)
.setPriority(NotificationCompat.PRIORITY_MAX) // Set whether the notification is important, Max indicates the most important, will pop up the interface
.build();
manager.notify(1, notification);
}
Copy the code
In order to be compatible with android8 and above, the following modifications are required
@ReactMethod
public void sendNotice(ReadableMap map, Callback callback){
MainActivity context = MainActivity.mainActivity;
Context activityContext = MainActivity.getContext();
String title = map.getString("title");
String text = map.getString("text");
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);
NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(activityContext,
createNotificationChannel(activityContext));
Notification notification = notificationCompatBuilder
.setContentTitle(title) / / title
.setContentText(text) / / content
.setWhen(System.currentTimeMillis()) // The time of the prompt
.setSmallIcon(android.R.mipmap.sym_def_app_icon) // Set the small graph
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), android.R.mipmap.sym_def_app_icon )) // Set the larger image
.setAutoCancel(true) // Cancel the current prompt after opening the prompt
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) // Set the prompt sound
.setDefaults(NotificationCompat.DEFAULT_ALL) // Set the LED light to blink
.setContentIntent(pi)
.setPriority(NotificationCompat.PRIORITY_MAX) // Set whether the notification is important, Max indicates the most important, will pop up the interface
.build();
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
public static String createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "channelId";
CharSequence channelName = "channelName";
String channelDescription ="channelDescription";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
// Set the description to a maximum of 30 characters
notificationChannel.setDescription(channelDescription);
// Whether the channel notification uses vibration
notificationChannel.enableVibration(true);
// Set the display mode
notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
} else {
return null; }}Copy the code
Personal Blog Address