Код: Выделить всё
`if (Capacitor.platform !== "web") {
this.registerPush();
}`
Код: Выделить всё
`private async registerPush() {
if (Capacitor.getPlatform() === "android") {
let permStatus = await PushNotifications.checkPermissions();
if (permStatus.receive === "prompt") {
permStatus = await PushNotifications.requestPermissions();
}
if (permStatus.receive !== "granted") {
throw new Error("User denied permissions!");
}
await PushNotifications.register();
} else if (Capacitor.getPlatform() === "ios") {
PushNotifications.requestPermissions().then((result) => {
if (result.receive === "granted") {
PushNotifications.register();
} else {
console.error("User denied push notification permissions!");
}
});
}
PushNotifications.addListener("registration", async (token) => {
console.log("My token: " + token.value);
const user = await this.authService.getUserInSession();
const decryptUser = Globals.decryptOjbectsInSession(user);
this.authService
.updateRegKey({
cpf_cnpj: decryptUser.usuario.cpfcnpj,
reg_key: token.value,
})
.subscribe((response: any) => {
console.log("response: ", response);
});
});
PushNotifications.addListener("registrationError", (error) => {
console.log("Error on registration: " + JSON.stringify(error));
});
PushNotifications.addListener(
"pushNotificationReceived",
(notification) => {
console.log("Push notification received: ", notification);
}
);
PushNotifications.addListener(
"pushNotificationActionPerformed",
(notification) => {
console.log(
"Push notification action performed",
notification.actionId,
notification.inputValue
);
this.router.navigate([notification.notification.data.url]);
}
);
}`
android (работает)НО для симулятора iOS (у меня нет физического для тестирования) он просто не работает.
ios добавляет прослушиватели, а ios не вызывает правильный обратный вызов
и я не знаю, как именно это работает, но похоже, что он вызывает другой обратный вызов. Видите ли, обратный вызов регистрации, который я создал, по-видимому, является «#11279332», а когда он вызывается, вызывается тот, который называется «#11279336». на Android они одинаковые (#102964981)
Я пробовал использовать обе документации, которые смог найти:
https://capacitorjs.com/docs/guides/push -notifications-firebase и https://ionicframework.com/docs/native/ ... ifications
установили конденсатор/push-notifications и использовали команду «npx cap sync»
создал проект Firebase, получил GoogleService-Info.plist, добавил в xCode, как его просили, а также добавил ключ авторизации от Apple в проект Firebase
добавил «pod Firebase/Messaging» в файл Pods и обновил с помощью «npx cap update ios»
также добавил «import Firebase» и «FirebaseApp.configure()» " в AppDelegate.swift и обе функции:
Код: Выделить всё
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
Messaging.messaging().token(completion: { (token, error) in
if let error = error {
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
} else if let token = token {
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token)
}
})
}`
`func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
NotificationCenter.default.post(name:
.capacitorDidFailToRegisterForRemoteNotifications, object: error)
}`
я создал профиль Apple с разрешением на push-уведомления и загрузил его в xcode,
профиль разработчика и xcode
я также включил возможность push-уведомлений внутри vscode .
Шагов много, и я считаю, что сделал их все. может кто-нибудь сказать мне, что я делаю не так?
Подробнее здесь: https://stackoverflow.com/questions/785 ... n-callback