应用程序关闭时通知不起作用
const 标记 = ["AAASDfsadfasdfaFDSDFSDLFJLSDAJF;DSJFAsdfdsfsdf"];
app.get('/send-notfication', (req, res) => {
var FCM = require('fcm-node');
var fcm = new FCM(serverKey);
var messageToSend = "Hi there this is message";
var message = {
to: token,
data: {
ar_message: messageToSend,
},
};
fcm.send(message, function(err, response){
if (err) {
console.log("Something has gone wrong!", err);
} else {
console.log("Successfully sent with response: ", response.results);
res.send("Successfully sent")
}
});
})
Run Code Online (Sandbox Code Playgroud)
问题是如何在其中设置通知标题?通知不起作用,应用程序既不在后台关闭,也不在前台关闭,为什么?
我需要使用 Firebase Cloud Messaging 在我的 iOS 应用程序中发出静默通知/后台通知,这样即使用户没有点击推送通知,我也可以在应用程序中进行更新。
Apple关于后台通知的文档在这里,据说是
要发送后台通知,请使用 aps 字典创建远程通知,该字典仅包含有效负载中的内容可用键
该文档中的后台通知的示例有效负载如下所示:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
Run Code Online (Sandbox Code Playgroud)
因此,我在使用云函数节点 JS 发送 FCM 时创建自己的有效负载。我的代码是这样的
const userToken = device.token
const payload = {
data: {
notificationID : notificationID,
creatorID : moderatorID,
creatorName: creatorName,
title : title,
body : body,
createdAt : now,
imagePath : imagePath,
type: type
},
apps : {
"content-available" : 1 // to force background data update in …Run Code Online (Sandbox Code Playgroud) 我遵循了两个建议安装以下内容的教程:
ionic cordova 插件添加 cordova-plugin-fcm-with-dependecy-updated npm install @ionic-native/fcm
安装到表示未安装 fcm 插件的设备后,我不断收到错误,因此我查看了建议卸载的https://www.npmjs.com/package/cordova-plugin-fcm-with-dependecy-updated#fcmontokenrefresh网站npm uninstall @ionic-native/fcm # 包含 Ionic 支持,但与 @ionic-native 的实现冲突。
所以我这样做并用这个代替:
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated';
getUserToken(userId: string) {
FCM.getToken().then(token => {
console.log('token');
console.log(token);
});
}
getToken(userId: string) {
FCM.onTokenRefresh((fcmToken: string) => {
console.log('token refresh');
console.log(fcmToken);
});
}Run Code Online (Sandbox Code Playgroud)
但现在我得到以下信息:
错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“onTokenRefresh”TypeError:无法读取未定义的属性“onTokenRefresh”
此时我被困住了。如果我使用 Ionic 插件,我会收到“未安装 FCMPluging”的信息,并且在使用 Cordova 库时会收到“无法读取未定义属性“onTokenRefresh”的错误信息。任何帮助将不胜感激。
{
"name": "",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng …Run Code Online (Sandbox Code Playgroud)cordova ionic-framework cordova-plugins firebase-cloud-messaging cordova-plugin-fcm
我想将 Google 云消息和通知添加到我的服务器。
我已将以下角色添加到我的服务帐户,但仍然无法从我的服务器发送消息。
Firebase Cloud Messaging Admin
Run Code Online (Sandbox Code Playgroud)
我更深入地挖掘了一下,发现我们需要获得许可才能使其发挥作用。
cloudmessaging.messages.create
Run Code Online (Sandbox Code Playgroud)
此权限在默认的所有访问帐户中可用,但如果我创建具有特定访问权限的新服务帐户,我在任何地方都找不到此权限。
请大家帮忙!
google-cloud-platform firebase-cloud-messaging firebase-notifications
我正在使用 FCM 和 SNS 测试推送通知。(SNS -> FCM (ios/android)。Android 工作没有问题。我可以触发从 SNS 到 FCM 的通知到我的 Android 设备。
\n然后,我测试了将 iOS 版本连接到 firebase 并添加了证书等,并且可以毫无问题地从 firebase 测试页面内触发推送通知到 iOS 设备。
\n问题是我可以\xe2\x80\x99t 使用 iOS 版本触发从 AWS SNS 到 FCM 的通知(Android 版本没有问题)。我希望它能像安卓一样工作。当触发来自 SNS 的通知时,是否需要将任何自定义参数添加到有效负载中才能使 iOS 版本正常工作,这些参数与 Android 版本不同?
\n这是我从 SNS 发送到 FCM 进行测试的自定义负载:
\n{\n"GCM": "{ "data": { "message": "Android 端点的示例消息" }, "content_available": true, "mutable_content": true} , "notification": {"body": "输入您的消息", "声音": "默认"}"\n}
\n我正在使用 Firebase FCM 将通知从我的服务器发送给用户。
当用户第一次安装应用程序时,我在MessagingService.java中捕获了新鲜的令牌:
@Override
public void onNewToken(@NonNull String tkn) {
super.onNewToken(tkn);
sendTokenToServer(tkn);
}
Run Code Online (Sandbox Code Playgroud)
问题来了,当用户关闭会话(不卸载应用程序)时,SharedPreferences 被删除。新的会话开始;但onNewToken()没有被调用。因此,我必须手动检索MainActivity中的令牌才能将其发送到服务器。我正在使用这段代码获取更新的令牌:
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
sendTokenToServer(instanceIdResult.getToken());
}
});
Run Code Online (Sandbox Code Playgroud)
如您所知,该代码已被弃用,应该避免使用。相反,我尝试用这段代码替换它,但没有成功:
FirebaseInstallations.getInstance().getToken(true).addOnCompleteListener(new OnCompleteListener<InstallationTokenResult>() {
@Override
public void onComplete(@NonNull Task<InstallationTokenResult> task) {
if(task.isSuccessful()) {
String token = task.getResult().getToken();
}
}
});
Run Code Online (Sandbox Code Playgroud)
在 onNewToken() 中获得的令牌长度是 163。在已弃用的调用中获得的令牌长度是 163(完美,但已弃用)。在 FirebaseInstallations 中获取的令牌长度为 316。
我在服务器端的 firebase API 无法使用 316 长度的代码发送通知。有人知道我做错了什么吗?或者为什么我会得到那些不同长度的令牌?
更新:
服务器端Python,从数据库检索令牌并发送通知,如下所示。请注意,当 token len 为 163 时,此代码有效。
from pyfcm import …Run Code Online (Sandbox Code Playgroud) 我今天遇到了一个问题。两三天前,我将 FCM 集成到我的 Flutter 应用程序中。虽然执行上有些欠缺,但我并不在意,因为我的编码阶段的 FCM 部分还没有到来。但今天,在开始编码之前,我通常会运行flutter run这些日志。
W/ConnectionTracker(17017): java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.measurement.internal.zzja@e8966f2
W/ConnectionTracker(17017): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1832)
W/ConnectionTracker(17017): at android.app.ContextImpl.unbindService(ContextImpl.java:1880)
W/ConnectionTracker(17017): at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
W/ConnectionTracker(17017): at com.google.android.gms.common.stats.ConnectionTracker.zza(com.google.android.gms:play-services-basement@@17.3.0:55)
W/ConnectionTracker(17017): at com.google.android.gms.common.stats.ConnectionTracker.unbindService(com.google.android.gms:play-services-basement@@17.3.0:50)
W/ConnectionTracker(17017): at com.google.android.gms.measurement.internal.zzjb.zzF(com.google.android.gms:play-services-measurement-impl@@18.0.2:6)
W/ConnectionTracker(17017): at com.google.android.gms.measurement.internal.zzil.zza(com.google.android.gms:play-services-measurement-impl@@18.0.2:5)
W/ConnectionTracker(17017): at com.google.android.gms.measurement.internal.zzak.run(com.google.android.gms:play-services-measurement-impl@@18.0.2:5)
W/ConnectionTracker(17017): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(17017): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(17017): at com.google.android.gms.measurement.internal.zzfh.run(com.google.android.gms:play-services-measurement-impl@@18.0.2:6)
E/FirebaseInstanceId(17017): Failed to get FIS auth token
E/FirebaseInstanceId(17017): java.util.concurrent.ExecutionException: com.google.firebase.installations.FirebaseInstallationsException: Firebase Installations Service is unavailable. Please try again later.
E/FirebaseInstanceId(17017): at com.google.android.gms.tasks.Tasks.zza(com.google.android.gms:play-services-tasks@@17.1.0:69)
E/FirebaseInstanceId(17017): at com.google.android.gms.tasks.Tasks.await(com.google.android.gms:play-services-tasks@@17.1.0:23)
E/FirebaseInstanceId(17017): at com.google.firebase.iid.GmsRpc.setDefaultAttributesToBundle(com.google.firebase:firebase-iid@@21.0.1:11)
E/FirebaseInstanceId(17017): at com.google.firebase.iid.GmsRpc.startRpc(com.google.firebase:firebase-iid@@21.0.1:1) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 VoIP 应用程序,但后台处理程序有问题。我想要的是:发送类型为“call”的推送,然后应用程序将显示呼叫 UI,稍后如果收到推送类型“hangup”,应用程序将关闭该呼叫 UI。
在后台处理程序中,我使用单例全局类来通知 StreamController 的挂断事件,并且 CallScreen 小部件将侦听该流以关闭自身。
然后我发现 flutterfire 将为后台处理程序启动另一个隔离(关闭屏幕),因此它将创建另一个单例类 -> 我无法使用这个新的单例类关闭我的 CallScreen UI。
是否可以用 flutterfire 的背景隔离做这样的事情?
伪代码示例:
class SingletonGlobal {
/// singleton class
final hangUpStreamController = StreamController<HangUpEvent>.broadcast();
void addHangupEvent(HangUpEvent event) {
hangUpStreamController.add(event);
}
}
class CallScreen extends StatefulWidget {
//// ...
@override
void initState() {
SingletonGlobal().hangUpStreamController.stream.listen((event) => closeCallUI(event));
}
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
await appInit();
if (message.data.type = 'hangup') {
SingletonGlobal().addHangupEvent(hangUpEvent);
}
Run Code Online (Sandbox Code Playgroud)
我尝试了 Nitrodon 的解决方案。但正如我提到的, _firebaseMessagingBackgroundHandler 将创建我的SingleGlobal类的另一个实例。我不知道为什么。 …
我的 (Flutter) 移动应用程序使用 Firebase 消息传递,并且在调试模式下工作正常,但是当我在发布模式下运行/构建它时,它会在收到来自 Firebase 的消息后崩溃。我怀疑不知onMessage.listen何故导致应用程序崩溃。它仅发生在 Android 设备上。
在 Firebase 项目设置或 Android 应用配置过程中是否有我可能错过的内容?
收到消息后会产生以下错误日志:
E/AndroidRuntime(26859): FATAL EXCEPTION: firebase-iid-executor
E/AndroidRuntime(26859): Process: com.company.app, PID: 26859
E/AndroidRuntime(26859): java.lang.IllegalAccessError: Illegal class access: 'b.c.a.b.d.a' attempting to access 'b.c.a.b.b.a.c' (declaration of 'b.c.a.b.d.a' appears in base.apk)
E/AndroidRuntime(26859): at b.c.a.b.d.a.<init>(Unknown Source:169)
E/AndroidRuntime(26859): at b.c.a.b.d.a.<init>(:1)
E/AndroidRuntime(26859): at com.google.firebase.messaging.l0.b(:1)
E/AndroidRuntime(26859): at com.google.firebase.messaging.Y.e(:2)
E/AndroidRuntime(26859): at com.google.firebase.messaging.m.call(:1)
E/AndroidRuntime(26859): at b.c.a.b.e.F.run(Unknown Source:4)
E/AndroidRuntime(26859): at com.google.firebase.messaging.n.execute(Unknown Source:0)
E/AndroidRuntime(26859): at b.c.a.b.e.m.c(Unknown Source:20)
E/AndroidRuntime(26859): at com.google.firebase.messaging.o.c(:1)
E/AndroidRuntime(26859): at b.c.a.b.a.a.b(:1)
E/AndroidRuntime(26859): at b.c.a.b.a.a.a(Unknown Source:21) …Run Code Online (Sandbox Code Playgroud) dart firebase flutter firebase-cloud-messaging flutter-dependencies
我想使用 Firebase Cloud Messaging 向用户发送通知,但当我尝试设置消息传递时收到此错误:
TypeError:未定义不是一个对象(评估“navigator.serviceWorker.addEventListener”)在node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6在reportException在node_modules/react-native/Libraries/Core/ExceptionsManager。 js:172:19 在 node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 在handleError 在node_modules/@react-native/polyfills/error-guard.js:49:36 在 ErrorUtils.reportFatalError在node_modules/metro-runtime/src/polyfills/require.js:204:6 在guardedLoadModule 在Screens/Tabs/Profile/UserData.js:16:4 在UserData
这是我的代码:
import { initializeApp, getApps, getApp } from "firebase/app";
import {getMessaging, getToken} from 'firebase/messaging';
const firebaseConfig = {
apiKey: "...",
appId: "...",
authDomain: "...",
projectId: "...",
messagingSenderId: "...",
storageBucket: "...",
};
export const firebaseApp = getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();
const messaging = getMessaging(firebaseApp);
getToken(messaging, { vapidKey: '...' }).then((currentToken) => {
if (currentToken) {
// Send the token to your server and …Run Code Online (Sandbox Code Playgroud) javascript push-notification firebase react-native firebase-cloud-messaging
firebase ×6
flutter ×3
android ×2
ios ×2
node.js ×2
amazon-sns ×1
cordova ×1
dart ×1
express ×1
javascript ×1
react-native ×1