Push Notification
Android
If you havent already done the setup in firebase and kommunicate dashboard, follow the steps below:
For firebase setup and to get you server key follow this section.
Then go to Kommunicate dashboard/Settings/Push notification and add the Server key obtained from firebase dashboard under GCM/FCM key and save.
Also download the google-services.json
file from the firebase dashboard and paste it under android folder.
Then refer to this section to add the FCM related files, incase you don't already have the code.
Finally, follow these steps:
Goto /android/build.gradle
- Add the following under buildscript -> dependencies
classpath 'com.google.gms:google-services:3.0.0'
- Add the following at the bottom of the file
apply plugin: 'com.google.gms.google-services'
After adding, it will look something like this:
buildscript {
repositories {
mavenCentral()
jcenter()
}
...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1'
classpath 'com.google.gms:google-services:3.1.1'
}
}
...
apply plugin: 'com.google.gms.google-services'
iOS
Upload your APNS certificates for both development and distribution under Kommunicate dashboard/settings/Push Notification/iOS section. And update capabilities in Xcode as described here.
Open the project in XCode, Create new file with name KommunicateWrapper
in YourApp module. Copy the code for KommunicateWrapper
from this link and paste it inside your newly created file.
Goto platforms/ios/YourApp/Classes and open AppDelegate.m
file, if the file does not exist create one, add the below code in the file:
#import "AppDelegate.h"
#import "MainViewController.h"
#import <UserNotifications/UserNotifications.h>
#import "YOUR_TARGET_NAME_HERE-Swift.h" /// Important: Replace 'YOUR_TARGET_NAME_HERE' with your target name.
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UNUserNotificationCenter.currentNotificationCenter setDelegate:self];
[KommunicateWrapper.shared application:application didFinishLaunchingWithOptions:launchOptions];
self.viewController = [[MainViewController alloc] init];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[KommunicateWrapper.shared applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[KommunicateWrapper.shared applicationWillEnterForeground:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[KommunicateWrapper.shared applicationWillTerminateWithApplication:application];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[KommunicateWrapper.shared application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[KommunicateWrapper.shared userNotificationCenter:center willPresent:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
completionHandler(options);
}];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
[KommunicateWrapper.shared userNotificationCenter:center didReceive:response withCompletionHandler:^{
completionHandler();
}];
}
@end
Updating Capabilities
Post setting up APNs, the next step is to enable push notifications within your project.
To do that click on your project and select your project from TARGETS
- Next select signing & capabilities section and click + to add below capabilities
- Push Notifications capability
- Background Modes
- Select Background Fetch and Remote notifications
- Select Background Fetch and Remote notifications
Following screenshot would be of help.
Using cordova plugin firebasex
If you are using cordova_plugin_firebasex plugin in your app for push notifications, follow the below steps to make Kommunicate notifications work with the Firebase plugin:
Copy the
FcmListenerService.java
file from here and paste it in your project's java directory project/android/app/src/main/java/Open
FcmListenerService.java
and extend it withFirebasePluginMessagingService
instead ofFirebaseMessagingService
public class FcmListenerService extends FirebaseMessagingService
- In the
FcmListenerService.java
file add the below import:
import org.apache.cordova.firebase.FirebasePluginMessagingService;
- In AndroidManifest.xml file add or replace the below service inside <application tag:
<service
android:name="org.apache.cordova.firebase.FirebasePluginMessagingService"
tools:node="remove" />
<service
android:name="com.applozic.mobicomkit.uiwidgets.KmFirebaseMessagingService"
tools:node="remove" />
<service
android:name=".FcmListenerService" //This is the name of your service
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Add the below tools entry (If not already added) inside the <manifest tag of AndroidManifest.xml file as below:
xmlns:tools="http://schemas.android.com/tools"
for example (2nd line in the below snippet):
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">