GuidesDiscussions
Log In
Guides

Firebase Messaging

Push Notifications

Android

FCM Integration in Android:

Create the methods for firebase:

@pragma('vm:entry-point')
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();
  Map<String, dynamic> parameters = Map();

  parameters["data"] = message.data;
  parameters["from"] = message.from;

  if (Platform.isAndroid) {
    _drpluginPlugin.invokeFCM(parameters);
  }
  print('Handling a background message ${message.messageId}');
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(const MyApp());
}

void listenFCM() async {
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    Map<String, dynamic> parameters = Map();
    parameters["data"] = message.data;
    parameters["from"] = message.from;

    if (Platform.isAndroid) {
      _drpluginPlugin.invokeFCM(parameters);
    }
  });
}

void getToken() async {
  await FirebaseMessaging.instance.getToken().then((token) {
    setState(() {
      _drpluginPlugin.refreshToken();
    });
  });
}

iOS

Request Push Notification Permission:

void requestNotifications() {  
  _drpluginPlugin.requestPushPermission();  
}  

App Tracking Permission:

DR SDK needs to request App Tracking permission from the user to collect IDFA details and track user activity on DR Platform. Below is the sample code to invoke the same from any View Controller class.

void requestAppTracking() {  
  _drpluginPlugin.requestAppTracking();  
}  

In case you have another push SDK or an implementation of your own, you might want to turn off DR SDK's method swizzling. DR SDK performs method swizzling on top of iOS' APNS delegate methods to capture relevant events from notifications and analytics. This means that when our SDK is present, it overrides whatever other APNS implementation already present in the app.

Disable Method Swizzling:
[Add this section to the web documentation: https://developers.digitalreef.com/docs/push-notifications#disable-method-swizzling]

Add APNS methods to your AppDelegate:
[Add this section to the web documentation

: https://developers.digitalreef.com/docs/push-notifications#add-apns-methods-to-your-appdelegate]