推送通知未正确连接到 Firebase 消息传递
Posted
技术标签:
【中文标题】推送通知未正确连接到 Firebase 消息传递【英文标题】:Push Notifications not connecting to firebase messaging properly 【发布时间】:2021-08-20 23:25:51 【问题描述】:我正在尝试在我的 Iphone 模拟器上测试推送通知。由于某种原因,我的测试似乎都没有通过。随着 firebaseMessaging 的新更新,我也无法弄清楚如何正确地将测试通知打印到我的终端以查看它是否连接正确。我是新来的,并且正在学习非常过时的 Udemy 课程。如果有人可以就我可能出错的地方提供一些建议,我将不胜感激。先感谢您。下面是我的 pushnotification service.dart 文件。
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:driver/datamodels/jobdetails.dart';
import 'package:driver/globalvariables.dart';
import 'dart:io';
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:driver/widgets/ProgressDialog.dart';
class PushNotificationService
final FirebaseMessaging fcm = FirebaseMessaging.instance;
Future initialize(context) async
if (Platform.isios)
fcm.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
FirebaseMessaging.onMessage.listen((event)
// fetchJobInfo(getJobID(message), context);
(Map<String, dynamic> message) async => fetchJobInfo(getJobID(message), context);
);
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
// fetchJobInfo(getJobID(message), context);
(Map<String, dynamic> message) async => fetchJobInfo(getJobID(message), context);
);
Future<String> getToken() async
String token = await fcm.getToken();
print('token: $token');
DatabaseReference tokenRef = FirebaseDatabase.instance.reference().child('drivers/$currentFirebaseUser.uid/token');
tokenRef.set(token);
fcm.subscribeToTopic('alldrivers');
fcm.subscribeToTopic('allusers');
String getJobID(Map<String, dynamic> message)
String jobID = '';
if(Platform.isandroid)
jobID = message['data']['job_id'];
else
jobID = message['job_id'];
print('job_id: $jobID');
return jobID;
void fetchJobInfo(String jobID, context)
//show please wait dialog
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) =>
ProgressDialog(status: 'Fetching Details...',),
);
DatabaseReference jobRef = FirebaseDatabase.instance.reference().child(
'jobRequest/$jobID');
jobRef.once().then((DataSnapshot snapshot)
Navigator.pop(context);
final assetAudioPlayer = AssetsAudioPlayer();
if (snapshot.value != null)
assetAudioPlayer.open(Audio('sounds/sounds_alert.mp3'));
assetAudioPlayer.play();
double destinationLat = double.parse(
snapshot.value['destination']['latitude'].toString());
double destinationLng = double.parse(
snapshot.value['destination']['longitude'].toString());
String destinationAddress = snapshot.value['destination_address'];
String paymentMethod = snapshot.value['payment_method'];
JobDetails jobDetails = JobDetails();
jobDetails.jobID = jobID;
jobDetails.destinationAddress = destinationAddress;
jobDetails.destination = LatLng(destinationLat, destinationLng);
jobDetails.paymentMethod = paymentMethod;
// showDialog(
// context: context,
// barrierDismissible: false,
// builder: (BuildContext context) => NotificationDialog(jobDetails: jobDetails,),
// );
);
【问题讨论】:
请看最新文档firebase.flutter.dev/docs/messaging/apple-integration 【参考方案1】:有一种方法可以让您使用 Xcode cli 手动将通知推送到模拟器,这种方式甚至看起来更容易测试 Flutter/FireBase 的通知。
首先您必须设置通知负载文件,例如notification.apns
。文件内容:
"Simulator Target Bundle": "<Your App bundle identifier>",
"gcm.message_id": "random string",
"aps":
"alert": "Push Notifications Test",
"sound" : "default",
"badge": 1,
,
"dataKey": "any data value to ise in the app"
关键是定义一个属性 gcm.message_id
- 因为 FireBase 消息服务会检查它并且只在它存在时执行回调。
一旦你准备好了,只需运行xcrun simctl push booted notification.apns
,所有的 FBM 回调都会被执行。
此外,您可以在此答案中了解有关模拟通知的更多信息:How can I test Apple Push Notification Service without an iPhone?
【讨论】:
【参考方案2】:请参阅下面的最新文档链接:-
Flutter Notifications
【讨论】:
您好 Bholendra,感谢您的回复。我已经尝试过了,但由于某种原因仍然无法正常工作。上面的代码中是否有任何内容可能导致您发现我做错了什么?【参考方案3】:已经回答了,但是有图片。您无法收到 iPhone 模拟器的通知。
对于 iOS;您必须拥有物理 iOS 设备才能接收消息。 Firebase 云消息传递与 Apple 推送通知服务 (APN) 集成,但 APN 仅适用于真实设备。
【讨论】:
以上是关于推送通知未正确连接到 Firebase 消息传递的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 云消息传递未在 iOS 14 上提供推送通知