How to Store Push Notification Alert Message in Userdefault

Change push notification alert message

Try change you notification service extension code:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here
// Convert received string
let data = bestAttemptContent.body.data(using: .utf8)!
// Apply encoded string
bestAttemptContent.body = String(data: data, encoding: .utf16)

contentHandler(bestAttemptContent)
}
}

Having issue with the alert of notification

1) When app is in background:

Correct format to appear notification when app is in background is in aps dictionary as:

{
"aps": {
"alert": {
"title": "",
"subtitle": "",
"body": “”
},
"badge": 1,
"sound": "default",
"content-available": 1,
}
// Other data here...
}

So, in your case update the payload for aps key as:

  "aps": {
"alert": {
"title": "CabuZZ",
"body": “new voice message”
},
"badge": 1,
"sound": "default",
"content-available": 1,
}

2) When app is in foreground:

You will receive the data in userInfo in following function, you need to show and handle the view manually. iOS will not present the view when app is in foreground.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

// Handle your view here (app in foreground)

}

ios handle pushnotification in background

For this case, you can't save the data in the 'database of [your] app'; instead you save the data in the database on your server, which is where the PushNotification was generated in the first place. Then, when your App is started, it queries your database to get the notification data.

IOS notification showing twice with default and my notification alert

 First uninstall the app and try it. It will work fine same problem i am facing.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = APPLICATION_ID;
configuration.clientKey = CLIENT_KEY;
configuration.server = AMAZON_SERVER;
}]];

[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

[Fabric with:@[[Digits class]]];

if([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]!=TRUE)
{
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//storedevice Type in standardUserDefaults
[self setDeviceType];
#here i am edited

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

}
else
{

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];

}

return YES;
}
// remove the below methods

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//[PFPush handlePush:userInfo];
if ([userInfo objectForKey:@"aps"]) {
NSMutableDictionary * apsData = [userInfo objectForKey:@"aps"];
NSString* alert = [apsData objectForKey:@"alert"];
...
...

UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
message: message
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];

[alertWindow show];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
[PFPush handlePush:userInfo];
}
}

Firebase iOS receive data from push notification

payload like this

{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}

Read the payload data

@available(iOS 10, *)

extension AppDelegate : UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo

if let aps = userInfo["aps"] as? NSDictionary
{
let alert = aps["alert"]as? NSString
let badge = aps["badge"] as? Int
}
completionHandler([.alert, .badge, .sound])

}

Do I need Push-Notification to remind the user of something

Check the Local notification APIs. It works like the remote notification, but does not require internet and lets you schedule the notification event.

You'll find some sample code here



Related Topics



Leave a reply



Submit