【问题标题】:Firebase Push Notification [duplicate]Firebase 推送通知 [重复]
【发布时间】:2016-07-08 07:09:34
【问题描述】:

我在我的 Android 应用中为推送通知实现了 Firebase。我实现了两项服务来注册令牌并在检测到通知时创建通知。当我的应用程序启动时它正在工作,但是当我的应用程序关闭时它不起作用。

public class FirebaseinstanceIdService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.e("Firebase", refreshedToken);
        MainActivity.setFirebaseToken(refreshedToken);
    }

}

public class MyFirebaseMessageService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Notification Message Body: " + remoteMessage.getData().get("title"));

        //Calling method to generate notification
        sendNotification(remoteMessage.getData());
    }

    //This method is only generating push notification
    //It is same as we did in earlier posts
    private void sendNotification(Map<String, String> notification) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(notification.get("title"))
            .setContentText(notification.get("body"))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

还有我的清单:

    <service
        android:name=".FirebaseinstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".MyFirebaseMessageService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

还有我的要求:

{
    "to": "ex1CtVz5bbE:APA91bHiM_BCun62A9iCobF1yV26Dwn-hYCDhkYPoWEG5ZdqH0P28UsE5q1v7QibwAX7AJ290-9en9L6f548_2b7WEbJ8RPEWlIotLczjjCP7xEOWqeJk6Iz44vilWYvdu4chPwfsvXD",
    "data": {
        "title": "Notification",
        "body": "Message",
    }
}

我已经在 StackOverflow 中找到了一些解决方案,但在我的情况下它不起作用。我有一个 API Rest 调用 API Request Post:https://fcm.googleapis.com/fcm/send

所以,我的问题是:Firebase 是否在应用关闭时处理推送通知以及如何处理?

感谢您的回答

【问题讨论】:

  • 您能否指定您尝试过但没有成功的 StackOverflow 解决方案?这将有助于缩小造成这种情况的原因。
  • 在 api 调用中,参数可以是通知对象或数据对象,我都尝试过,但它不起作用。
  • 添加你的 manifest.xml
  • 不要同时尝试两者。仅使用data
  • 是的,蒂姆,我只使用数据。 Saini 我用我的清单编辑我的帖子

标签: android firebase firebase-cloud-messaging


【解决方案1】:

FCM 确实支持应用关闭时的通知。您的代码似乎没问题,所以我想电池节能器(或节电器)可以杀死您的通知。我在华硕 Zenfone 上遇到过这样的问题,在使用华为和小米的情况下也有报告。只需禁用它们或将您的应用程序添加到例外列表中(如果有的话)。在最近的 Android 版本中还有一个新的省电模式,请尝试禁用它。

【讨论】:

猜你喜欢
  • 2016-12-06
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 2018-09-19
相关资源
最近更新 更多