当应用程序背景时,通知具有不同的行为

时间:2016-11-16 10:41:59

标签: android mobile firebase notifications

我遇到了有关通知的问题。我的应用中有一项服务可以收听来自Firebase的消息。这是我的代码:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // super.onMessageReceived(remoteMessage);

    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    displayNotification(remoteMessage.getNotification().getBody());
}


private void displayNotification(String message) {
 if (message.contains("upgrade")) {
        upgradeClient(message);
    }

}

private void upgradeClient(String message) {

    final String appPackageName = getString(R.string.app_package);
    String url;
    try {
        url = "market://details?id=" + appPackageName;
    } catch (final Exception e) {
        url = "https://play.google.com/store/apps/details?id=" + appPackageName;
    }

    //Open the app page in Google Play store:
    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(message)
            .setSmallIcon(R.drawable.ic_local_notification)
            .setSound(notificationSound)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());

}

}

当应用程序被预设时,通知会完全应该执行(在这种情况下,将用户带到Play商店)。问题是应用程序是否背景。在应用程序后台运行时点击通知会导致应用程序启动,用户不会被带到商店。 upgradeClient()甚至没有被调用!提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果您正在接受服务,请查看该消息

字符串消息可能为空。

class Item {
    int id;
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="name")
    @JsonIdentityReference(alwaysAsId=true)
    ItemType itemType;
}