I am attempting to test out push notifications on my Iphone simulator. For some reason none of my tests seems to be going through. With the new updates on firebaseMessaging I also cannot figure out how to properly print the test notification to my terminal to see if it is connected properly. I am new to flutter and following a very outdated Udemy class. If anyone could maybe give some advice on where I may be going wrong, I would greatly appreciate it. Thank you in advance. Below is my pushnotification service.dart file.
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,),
// );
}
});
}
}