Integration

Flutter is a cross-platform that lets you create android and iOS apps from a single code base by using a modern, and reactive framework. These apps are built using Dart, a simple object-oriented programming language. The basic idea of Flutter revolves around widgets. Flutter entails its own ready-made widgets which appear native either to Android (Material Design) or iOS apps (Cupertino). Also, you can create customized widgets.

Requirements :

Create a NotifyVisitors Account if you do not already have one.

Brand ID and Secret Key from NotifyVisitors Account.

Android : FCM integration

IOS : Push Notification Certificate

Configuration google-service.json (Android Push Notification)

Firstly, navigate to FCM and register your app.

Copy the Sender Id and Legacy server key from FCM.

Download the latest config JSON file (google-service.json) and paste it in the main folder at
project/android/src/main path.

Next, Go to the Notifyvisitors panel > App push menu > Android inside configuration section, from the left side
menu panel, and Paste Sender Id and Legacy server key there.

Add the below permission in your app-level build.gradle in the bottom. (/build.gradle):

apply plugin: 'com.google.gms.google-services'

Now, Add the below lines Project-level build.gradle (/build.gradle)

buildscript {
  dependencies {
   	 // Add this line
    	classpath 'com.google.gms:google-services:4.0.1'
  }
}

Configuration of Push Notification Icon (Android Notification Icon)

Create a monochrome png icon and the size must be 200*200.

Name of the icon should be sm_push_logo.png.

Copy into your-project/android/app/src/main/res/drawable/

Add NotifyVisitors Library to your Project

$ flutter pub add flutter_notifyvisitors

/* this will add a line like this to your package's pubspec.yaml 
  dependencies:
    flutter_notifyvisitors: ^1.0.0
*/

INITIALIZE SDK (Platform Specific)

ANDROID

  1. Add these permissions to your app manifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  1. Add bid and bid_e to your app manifest.xml file.
<meta-data
           android:name="notifyvisitors_bid"
           android:value="XXXX" />
 <meta-data
           android:name="notifyvisitors_bid_e"
           android:value="XXXXXXXXXXXXXXXXXXXXXXXXX" />

📘

Note: In the above example, Dummy Brand ID and Encryption keys are shown. Kindly login your NotifyVisitors account to see your credentials.

  1. Main application class initialization.

a. If you don’t have your Application class, then add this line on your manifest.xml. Otherwise, skip this step
and follow next.

android:name="com.notifyvisitors.notifyvisitors.NotifyVisitorsApplication"

b. If you have the Application class, Initialize flutter-notifyvisitors plugin as follows.

i. Import this statement

import com.flutter.notifyvisitors.NotifyvisitorsPlugin;

ii. Call InviteReferrals register() method in onCreate() method.

NotifyvisitorsPlugin.register(this);

IOS INTEGRATION

  1. Run this command from the terminal.
cd ios && pod install && cd ..
  1. Next, Configure info.plist file.
<key>NSAppTransportSecurity</key>
     <dict>
       <key>NSAllowsArbitraryLoads</key>
        <true/>
       <key>NSExceptionDomains</key>
     </dict>
   <key>UIBackgroundModes</key>
   <array>
       <string>fetch</string>
       <string>remote-notification</string>
   </array>
   <key>CFBundleURLTypes</key>
   <array>
       <dict>
       <key>CFBundleURLName</key>
           <string>"Your Project Identifier Name"</string>
       <key>CFBundleURLSchemes</key>
           <array>
           <string>"yourURLscheme"</string>
           </array>
       </dict>
   </array>
   <key>nvBrandID</key>
       <integer>Your BRANDID comes here</integer>
   <key>nvSecretKey</key>
       <string>Your SECRET KEY comes here</string>
   <key>nvPushCategory</key>
       <string>nvpush</string>
   <key>nvViewAutoRedirection</key>
       <true/>  <!--OR--> <!-- <false/>-->

OR

You can simply open the info.plist and add the keys which works as same as above for this,

Add a new row by going to the menu and clicking Editor > Add Item. Setup a NSAppTransportSecurity as a Dictionary.

Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES as follows.

Add a new row again and set up a URL Types item by adding a new item. Expand the URL Types key, expand Item 0, and add a new item, URL schemes. Fill in "appScheme" for Item 0 of URL schemes and your company identifier for the URL Identifier. Your file should resemble the image below when done.

(Optional) Add a new row again and set up an NSLocationAlwaysAndWhenInUseUsageDescription as String and fill this field with the message you want to show in the alert box for asking user permission for geofencing (example: location is required for geofence).

Add a new row again and set up a nvBrandID as Number and fill this field with your BRAND ID .

Add a new row again and set up a nvSecretKey as String and fill this field with your SECRET KEY.

Add a new row again and set up a nvPushCategory as String and set its value nvpush.

Add a new row again and set up a key nvViewAutoRedirection as Boolean set it, YES to enable auto redirection of your app’s ViewControllers from SDK or set it as NO to handle redirections by your app.

📘

Note: In the above example Dummy Brand ID and Secret keys shown. Kindly login your NV_account to see your credentials

  1. Configure Capabilities

Go to Capabilities Tab and turn on the Background Modes. To do that, select the project from the Project
Navigator
and then select the app target and Capabilities tab, and turn the Background Modes "switch on".
Select 3 checkboxes (i.e. Location updates, Background fetch, Remote notifications).

In the Capabilities Tab Turn On Push Notifications and if you have upgraded Xcode 8.0 or if you have turned on Push Notifications in the previous version, kindly turn it off in order to configure push notification properly for the upgraded devices.

In the Capabilities Tab Turn On App Groups and click on + sign and add a new app group. Assign the group name as group.nv.{Your App Bundle Identifier}.

Example:

If Your App’s Bundle Identifier is com.example.myapp, then the App Group must be named as
group.nv.com.example.myapp. Make sure this newly created app group must be turned on.

Configure App Delegate Methods

A. Include the below header file of your AppDelegate.h.

#import <flutter_notifyvisitors/NotifyvisitorsPlugin.h>
#import "NotifyvisitorsPlugin.h"

B. Initialise the SDK in application didFinishLaunchingWithOptions function.

[NotifyvisitorsPlugin nvInitialize];
NotifyvisitorsPlugin.nvInitialize()

C. Call this method in application didFinishLaunchingWithOptions to register your app for push notification.

[NotifyvisitorsPlugin registerPushWithDelegate:self  App:application launchOptions:launchOptions];
NotifyvisitorsPlugin.registerPush(withDelegate: self, app: application, launchOptions: launchOptions)

Example :

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GeneratedPluginRegistrant registerWithRegistry:self];
    // Override point for customization after application launch.
    
    [NotifyvisitorsPlugin Initialize];
    [NotifyvisitorsPlugin RegisterPushWithDelegate:self App:application launchOptions:launchOptions];
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    
    NotifyvisitorsPlugin.nvInitialize()
    NotifyvisitorsPlugin.registerPush(withDelegate: self, app: application, launchOptions: launchOptions)

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

D. Add the following three functions inside your AppDelegate file to handle the registering and receiving events of push notifications.

[NotifyvisitorsPlugin application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
NotifyvisitorsPlugin.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)

Example :

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [NotifyvisitorsPlugin application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    NotifyvisitorsPlugin.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
 }

E. didFailToRegisterForRemoteNotificationsWithError

[NotifyvisitorsPlugin application:application didFailToRegisterForRemoteNotificationsWithError:error];
NotifyvisitorsPlugin.application(application, didFailToRegisterForRemoteNotificationsWithError: error)

Example :

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    [NotifyvisitorsPlugin application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
   NotifyvisitorsPlugin.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
 }

F. didReceiveRemoteNotification

[NotifyvisitorsPlugin application:application didReceiveRemoteNotification:userInfo];
NotifyvisitorsPlugin.application(application, didReceiveRemoteNotification: userInfo)

Example :

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [NotifyvisitorsPlugin application:application didReceiveRemoteNotification:userInfo];
}
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
  NotifyvisitorsPlugin.application(application, didReceiveRemoteNotification: userInfo)
}

G. applicationDidEnterBackground

[NotifyvisitorsPlugin applicationDidEnterBackground:application];
NotifyvisitorsPlugin.applicationDidEnterBackground(application)

Example :

- (void)applicationDidEnterBackground:(UIApplication *)application{
    [NotifyvisitorsPlugin applicationDidEnterBackground:application];
}
override func applicationDidEnterBackground(_ application: UIApplication) {
  NotifyvisitorsPlugin.applicationDidEnterBackground(application)
 }

H. applicationDidBecomeActive

[NotifyvisitorsPlugin applicationDidBecomeActive:application];
NotifyvisitorsPlugin.applicationDidBecomeActive(application)

Example :

- (void) applicationDidBecomeActive:(UIApplication *)application{
    [NotifyvisitorsPlugin applicationDidBecomeActive:application];
}
override func applicationDidBecomeActive(_ application: UIApplication) {
   NotifyvisitorsPlugin.applicationDidBecomeActive(application)
 }

I. applicationWillTerminate

[NotifyvisitorsPlugin applicationWillTerminate];
NotifyvisitorsPlugin.applicationWillTerminate()

Example :

- (void) applicationWillTerminate:(UIApplication *)application{
    [NotifyvisitorsPlugin applicationWillTerminate];
}
override func applicationWillTerminate(_ application: UIApplication) {
   NotifyvisitorsPlugin.applicationWillTerminate()
 }

J. openURL

[NotifyvisitorsPlugin openUrl:application openURL:url];
NotifyvisitorsPlugin.openUrl(app, open: url)

Example :

-(BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
    [NotifyvisitorsPlugin openUrl:application openURL:url];
    return YES;
}
override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
     NotifyvisitorsPlugin.openUrl(app, open: url)
     return true
 }

K. notifyVisitorsGeofencingReceivedNotificationWithApplication (For Geofencing Push Notification Only)

[NotifyvisitorsPlugin notifyVisitorsGeofencingReceivedNotificationWithApplication:application localNotification:notification];
NotifyvisitorsPlugin.notifyVisitorsGeofencingReceivedNotification(with: application, localNotification: notification)
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *) notification {
    [NotifyvisitorsPlugin notifyVisitorsGeofencingReceivedNotificationWithApplication:application localNotification:notification];
}
override func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
     NotifyvisitorsPlugin.notifyVisitorsGeofencingReceivedNotification(with: application, localNotification: notification)
  }

iOS 10 Push Notification Handler

For iOS 10 Push Notification

Add the following Delegate Methods in the AppDelegate file.

  1. Go to AppDelegate.h to import UserNotifications framework. Further, add UNUserNotificationCenterDelegate to support iOS 10 Push Notification or Rich media push notifications.
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
@end
  1. Now go to AppDelegate.m file and add the following delegate methods to handle iOS 10 push notifications.
[NotifyvisitorsPlugin willPresentNotification:notification withCompletionHandler:completionHandler];
NotifyvisitorsPlugin.willPresent(notification, withCompletionHandler: completionHandler)

Example :

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    [NotifyvisitorsPlugin willPresentNotification:notification withCompletionHandler:completionHandler];  
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
   NotifyvisitorsPlugin.willPresent(notification, withCompletionHandler: completionHandler)
 }
[NotifyvisitorsPlugin didReceiveNotificationResponse:response];
NotifyvisitorsPlugin.didReceive(response)

Example :

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    [NotifyvisitorsPlugin didReceiveNotificationResponse:response];
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
     NotifyvisitorsPlugin.didReceive(response)
 }
[NotifyvisitorsPlugin application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
NotifyvisitorsPlugin.application(application, didReceiveRemoteNotification: userInfo)

Example :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [NotifyvisitorsPlugin application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
   NotifyvisitorsPlugin.application(application, didReceiveRemoteNotification: userInfo)
 }

Import NotifyVisitors package

import 'package:flutter_notifyvisitors/flutter_notifyvisitors.dart';