Push Notification
Android
If you haven't 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.
Now 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. The <CLASS_PACKAGE>
, for the manifest, by default will be blank. Simply remove it.
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/Classes. Copy the code for KommunicateWrapper
from this link and paste it inside your newly created file.
Goto /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
Register push notification
From login success callback, call this function:
RNKommunicateChat.registerPushNotification((response, responseMessage) => {
if (response == "Success") {
console.log(responseMessage)
}
});