在应用开始时检索Firebase通知

时间:2017-06-12 14:47:11

标签: android firebase push-notification firebase-cloud-messaging

我知道,当应用程序被杀时,它无法显示任何Firebase通知......或者这是我的新应用程序发生的情况:我杀了应用程序,新的通知也没有出现。

现在,我希望以一种新的方式解决这个问题:我希望在我的应用启动时检索应用“忘记”看到的所有通知。

这是我的代码

IdService

  public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

和消息类

   public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        notifies(remoteMessage.getNotification().getBody());
    }

    private void notifies(String body){
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MyFirebaseMessagingService.this)
                        .setSmallIcon(android.R.drawable.ic_menu_help)
                        .setContentTitle("Congratulations!")
                        .setContentText(body);
        Notification mNot= mBuilder.build();
// Display
        NotificationManager mNotMan = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        mNotMan.notify(1,mNot);
    }

}

你能给我什么建议?感谢所有答案:D

1 个答案:

答案 0 :(得分:0)

恕我直言,最好的方法是创建一个通知中心服务器端。

因此,每次发送推送通知时都会将其保存在服务器上。 当您的应用启动时,远程获取推送通知列表。