android通知 - 如何不在前台显示

时间:2017-01-27 18:00:12

标签: android notifications firebase-cloud-messaging

嘿,我遇到了一个棘手的问题,需要建议。我在收到FCM数据有效负载后手动构建了一个通知。这是自从数据有效载荷以来在前台和后台创建通知的方式:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {

        String msg = remoteMessage.getData().get("message");
        sendNotification(msg);
    }

private PendingIntent createIntent(String msg) {
    Intent intent = new Intent(this, SportsActivity.class);
    Bundle b = new Bundle();
        b.putString(Constants.KEY_GO_TO_TAB, Constants.KEY_DASHBOARD_HOCKEY_SCORE_TAB);
    intent.putExtras(b);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    return pendingIntent;
}

private void sendNotification( String messageBody) {
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setContentText(messageBody)
            .setColor(ContextCompat.getColor(this, R.color.hockey_brand))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(createIntent(messageBody));

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

}

它看起来功能正常。我遇到的问题是,当应用程序位于前台时,我希望通知不显示。没有扫描活动任务,有没有办法做到这一点?因为这需要许可

我已经阅读了this article,但这是如何知道你从前台到后台的。也弃用了android.permission.GET_TASKS,并且REAL_GET_TASKS权限也不适用于第三方。我只是想知道在任何给定时间用户是在前台或后台,所以我知道我是否应该显示通知。我想知道firebase本身是否有东西。当您发送"通知有效载荷"来自firebase控制台,如果应用程序不在前台,则不会在通知面板中显示,因此应该有办法执行此操作。

1 个答案:

答案 0 :(得分:1)

或者,您可以选择不在应用处于前台时通过实施Application.ActivityLifecycleCallbacks来显示通知 在你的申请中。

然后,您可以检查您的应用是在前台还是后台。

Source