当应用未运行或在后台运行时,在锁定屏幕上显示抬头通知和通知

时间:2018-11-30 11:17:40

标签: android

我想在应用未运行或在后台运行时显示抬头通知,而在应用正在运行且处于前台时显示当前的抬头通知。 通知到达时,还会在锁定屏幕和唤醒屏幕上显示通知。下面是我的代码:

FirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("Firebase msg", "onMessageReceived: " + remoteMessage);
    String title = remoteMessage.getNotification().getTitle();
    String body = remoteMessage.getNotification().getBody();
    NourishNotificationManager.getInstance(getApplicationContext()).displayNotification(title, body);
}

@Override
public void onNewToken(String token) {
    Log.d("Token", "Refreshed token: " + token);
    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
}

}

我的Singleton NotificationManager.java

public class NourishNotificationManager {
public static int NOTIFICATION_REQUEST_CODE = 100;
public static int NOTIFICATION_ID = 01;
Context mContext;
public static NourishNotificationManager mInstance;

public NourishNotificationManager(Context mContext) {
    this.mContext = mContext;
}

public static synchronized NourishNotificationManager getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new NourishNotificationManager(context);
    }
    return mInstance;
}

public void displayNotification(String title, String body) {
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNELID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title)
            .setContentText(body)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setLights(255,0,0)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);


    builder.setVibrate(new long[]{1000, 1000, 1000});

    Intent intent = new Intent(mContext, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, NOTIFICATION_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    if (mNotificationManager != null) {
        mNotificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

}

奥利奥检查

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel mChannel = null;
        mChannel = new NotificationChannel(CHANNELID, CHANNELNAME, NotificationManager.IMPORTANCE_HIGH);
        mChannel.setDescription(CHANNELDESCRIPTION);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mNotificationManager.createNotificationChannel(mChannel);
    }

0 个答案:

没有答案