skip to Main Content

I’m trying to send a message, but unfortunately I don’t receive any notification, I tried three attempts, but none of them reached my phone

Here is all the information that might be useful to you –
bundle id – com.atlas.ecoCity

my background model is –
Background models

my Google Service Info –

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CLIENT_ID</key>
    <string>753631636992-qg1l63ttslllc8kgug0j0rn6gpk3hqkq.apps.googleusercontent.com</string>
    <key>REVERSED_CLIENT_ID</key>
    <string>com.googleusercontent.apps.753631636992-qg1l63ttslllc8kgug0j0rn6gpk3hqkq</string>
    <key>ANDROID_CLIENT_ID</key>
    <string>753631636992-n9lpns306gu7q44f5lch8cokshaa1juk.apps.googleusercontent.com</string>
    <key>API_KEY</key>
    <string>AIzaSyAxFNhRGma2x9xkAhgLwX0pw--CNiiIYGg</string>
    <key>GCM_SENDER_ID</key>
    <string>753631636992</string>
    <key>PLIST_VERSION</key>
    <string>1</string>
    <key>BUNDLE_ID</key>
    <string>com.atlas.ecoCity</string>
    <key>PROJECT_ID</key>
    <string>eco-city-7f66a</string>
    <key>STORAGE_BUCKET</key>
    <string>eco-city-7f66a.appspot.com</string>
    <key>IS_ADS_ENABLED</key>
    <false></false>
    <key>IS_ANALYTICS_ENABLED</key>
    <false></false>
    <key>IS_APPINVITE_ENABLED</key>
    <true></true>
    <key>IS_GCM_ENABLED</key>
    <true></true>
    <key>IS_SIGNIN_ENABLED</key>
    <true></true>
    <key>GOOGLE_APP_ID</key>
    <string>1:753631636992:ios:21e0bc6953671795b032e5</string>
</dict>
</plist>

My main.dart code is

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
   // If you're going to use other Firebase services in the background, such as Firestore,
   // make sure you call `initializeApp` before using other Firebase services.
   await Firebase.initializeApp();

   print("Handling background message : ${message.messageId}");
}

future<void> main() async {
   WidgetsFlutterBinding.ensureInitialized();

   await Firebase.initializeApp(
     name: 'eco-city-deeplink',
     options: DefaultFirebaseOptions.currentPlatform,
   );

   //Request for permission -
   FirebaseMessaging messaging = FirebaseMessaging.instance;

   NotificationSettings settings = await messaging.requestPermission(
     alert: true
     announcement: false
     badge: true
     carPlay: false
     criticalAlert: false
     provisional: false
     sound: true
   );

   if (settings.authorizationStatus == AuthorizationStatus.authorized) {
     print('User granted permission');
   } else if (settings.authorizationStatus == AuthorizationStatus.provisional) {
     print('User granted temporary permission');
   } else {
     print('User denied or did not accept permission');
   }

   //Messages in the foreground
   FirebaseMessaging.onMessage.listen((RemoteMessage message) {
     print('Got a message while in the foreground!');
     print('Message data: ${message.data}');

     if (message.notification != null) {
       print('The message also contained a notification: ${message.notification}');
     }
   });

   FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

   runApp(MyApp());
}

Settings cloud messaging in firebase –
setting cloud messaging

my key in developer.apple – key in developer.apple

my profiles in developer.apple – profiles in developer.apple

I tried to wait a very long time, but not a single notification came to me.
PS – Yes, I’m testing on a real device

And my testing messaging – testing cloud messaging

Tell me what I did wrong? Why am I not receiving test messages?

PS: Apparently, it should be clarified, I’m trying to send not to one device, but to everything that received a request to send notifications when the application starts

my AppDelegate.swift –

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

2

Answers


  1. just for the future, try to put [redacted] or something at the place where your API-key is in the config. This would prevent someone else from stealing it and sending messages on your behalf or worse.

    Would like to ask, if you registered your phone to FCM by calling the getToken function?
    https://firebase.google.com/docs/cloud-messaging/flutter/client?hl=en#access_the_registration_token

    // It requests a registration token for sending messages to users from your App server or other trusted server environment.
    String? token = await messaging.getToken();
    
    if (kDebugMode) {
      print('Registration Token=$token');
    }
    

    This call would print the token in the console while debugging. It is useful, if you want to target your phone directly, but also necessary to make your phone "targetable" at all.

    Please let me know if this fixes your problem.

    Best regards

    Login or Signup to reply.
  2. please check this configuration for your AppDelegate.swift

    import UIKit
    import Flutter
    import Firebase
    
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
        FirebaseApp.configure()
        GeneratedPluginRegistrant.register(with: self)
        if #available(iOS 10.0, *) {
          UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
          }
       
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search