NativeScript / NativeScript/firebase

[@nativescript/firebase-messaging-core][iOS] Notification token is regenerated after app is killed

未关闭
#276 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
TypeScript
星标
62
派生
53
平均合并
8 天 4 小时
30 天内合并 PR
2

描述

There is a weird behavior with our app about the notification token in iOS only.
We have search everywhere to find for a fix for this issue but we didn't find anything.
When I launch the first time the app, I get the notification token with firebase().messaging().getToken(), then we save the token in our user database and send notification from our server.
The first time the app is launch, the notification are not received, it only works after killing the app and relaunch it again.

But that's not it!
After the second launch, if I kill the app and then launch it again, the token change!!
firebase().messaging().getToken() is called twice, the first one return the old token and the second one return the new token
According to firebase documentation, the token only change if the app is uninstalled or data is reset which is not the case

package.json:

{
  "name": "tina",
  "main": "./src/main.ts",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@angular/animations": "~14.3.0",
    "@angular/common": "~14.3.0",
    "@angular/compiler": "~14.3.0",
    "@angular/core": "~14.3.0",
    "@angular/forms": "~14.3.0",
    "@angular/platform-browser": "~14.3.0",
    "@angular/platform-browser-dynamic": "~14.3.0",
    "@angular/router": "~14.3.0",
    "@nativescript/angular": "^14.2.8",
    "@nativescript/camera": "^5.1.1",
    "@nativescript/core": "~8.5.0",
    "@nativescript/firebase-core": "^2.3.4",
    "@nativescript/firebase-messaging": "^2.3.4",
    "@nativescript/firebase-messaging-core": "^2.3.4",
    "@nativescript/geolocation": "^8.3.1",
    "@nativescript/imagepicker": "^3.1.1",
    "@nativescript/local-notifications": "^6.1.1",
    "@nativescript/mlkit-barcode-scanning": "^2.1.0",
    "@nativescript/mlkit-core": "^2.1.0",
    "@nativescript/theme": "~3.0.2",
    "nativescript-webview-interface": "^1.4.5",
    "rxjs": "~7.5.0",
    "zone.js": "~0.11.5"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.2.13",
    "@angular/compiler-cli": "~14.3.0",
    "@nativescript/android": "8.5.0",
    "@nativescript/ios": "8.6.4",
    "@nativescript/types": "~8.5.0",
    "@nativescript/webpack": "5.0.14",
    "@ngtools/webpack": "^14.2.13",
    "@types/node": "^14.18.63",
    "typescript": "^4.6.2"
  }
}

main.ts

import { platformNativeScript, registerElement, runNativeScriptAngularApp } from '@nativescript/angular';
import '@nativescript/firebase-messaging'

import { AppModule } from './app/app.module';
import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';
import { AuthorizationStatus } from '@nativescript/firebase-messaging';
import { setString } from '@nativescript/core/application-settings';
import { requestPermissions } from '@nativescript/camera';
import * as geolocation from '@nativescript/geolocation';
import { isIOS } from "@nativescript/core";
import { MLKitView } from '@nativescript/mlkit-core';

runNativeScriptAngularApp({
  appModuleBootstrap: () => platformNativeScript().bootstrapModule(AppModule),
});

try {
  registerElement('MLKitView', () => MLKitView);
  firebase().initializeApp().then(
    () => {
      firebase().messaging().requestPermission({
        ios: {
          alert: true,
        },
      }).then((p) => {
        if(p == AuthorizationStatus.AUTHORIZED || p == AuthorizationStatus.PROVISIONAL){
          firebase().messaging().registerDeviceForRemoteMessages();
          firebase().messaging().showNotificationsWhenInForeground = true;
          firebase().messaging().getToken().then(token => {
            setString("notificationToken", token);
          })
        }
      }).finally(() => {
        if(isIOS){
          requestPermissions().finally(() => { // for camera
            geolocation.isEnabled().then((isEnabled) => { //for gps
              if(!isEnabled){
                geolocation.enableLocationRequest(true).then(() => {
                });
              }
            })
          })
        }
      })
      console.log("firebase.init done");
    },
    error => {
      console.log(`firebase.init error: ${error}`);
    }
  );
}
catch (err) {
  console.log("err in try and catch " + err);
}

Let me know if you need something else to provide.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 main.ts 开始,跟踪初始化期间的 firebase().messaging().registerDeviceForRemoteMessages() 和 getToken()。重现 iOS 的首次启动和后续启动,检查 package.json 中的包版本以及两个 token 结果。当通知在首次启动时正常工作,并且应用终止后重新启动时 getToken() 仍保持稳定,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
ios, typescript
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。