1

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,),
        // );
      }
    });
  }
}
 
Ryan
  • 27
  • 7

3 Answers3

4

There is a way how you can push notifications to simulator manually with Xcode cli and this way even looks easier to test notifications for Flutter/FireBase.

First you have to set up notification payload file e.g. notification.apns. The contents of the file:

{
    "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"
}

The key is defining a property gcm.message_id - because FireBase Messaging service checks for it and only executes callbacks if it is present.

Once you have it ready, just run xcrun simctl push booted notification.apns and all the FBM callbacks will be executed.

Also, you can learn more about simulating notifications in this answer: How can I test Apple Push Notification Service without an iPhone?

xb1itz
  • 1,022
  • 10
  • 17
0

Please see the latest doc link is below:-

Flutter Notifications

enter image description here

Bholendra Singh
  • 980
  • 7
  • 14
  • Hi Bholendra, Thank you for your response. I have tried that already but still can't get it to work for some reason. Is there anything in my code above that may lead you to where I'm doing something wrong? – Ryan Jun 03 '21 at 01:37
0

It is already answered, but with a picture. You can't get notifications to an iPhone simulator.

For iOS; you must have a physical iOS device to receive messages. Firebase Cloud Messaging integrates with the Apple Push Notification service (APNs), however APNs only works with real devices.

Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30