Hi i i have a local push notification problem.
I implemented in-app language change function in my app.
and i have to restart the app after changed for reflect changes all part of my app. so i use abort() method.
before abort app i scheduled notification like this
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:#"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:#"Languaged changed. Touch to restart." arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:#"FiveSecond" content:content trigger:trigger];
// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:nil];
abort();
i want to user come back to app by touching alert
but push message didn't come at all the time. sometimes it was came
and doesn't work again.
please help me if you know something about this issue.
Call abort() in completion handler:
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
abort();
}];
But anyway, calling abort() isn't good practice.
You shall not exit app (someone already posted it here https://stackoverflow.com/a/356342/6429711 and I believe Apple have it somewhere in guidelines).
You can change language without quitting app. When you generate app multiple bundles are generated. You can change localisation from separate bundles then.
Example:
extension String
{
// Get selected language
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: currentLanguageBundle, value: "", comment: "")
}
}
Where currentLanguageBundle is bundle based on the currently selected language.
Related
I need to display a notification when my app is in background or killed. I have no problem to display FCM 'notification' message on iOS using firebase-cpp-sdk, I tried quickstart-cpp/messaging/testapp and it just worked. But when 'data' message is received when app is in background or foreground - no notification is displayed, I just see it in the log that message is received.
I use "content_available": true in message as suggested by many answers. This helps to actually receive the 'data' message, as I can see in the log, but the message is not displayed.
I tried legacy HTTP and HTTP v1 protocols, the result is the same.
An example of legacy HTTP message is:
{
"data": {
"body": "Entrance door Intrusion at 2 Jun 2020 01:32:08",
"title": "Intrusion"
},
"content_available": true,
"to": "fcm_device_token_here"
}
Do I need to manually create a notification like on Android? Or there are some other ways to do it?
To asnwer my own question - yes, I created a local notification. For that I used qt-notification library since I am using QT, but the code example to show notification is the following (taken from the project):
// create content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = title.toNSString(); // your title
content.body = caption.toNSString(); // your notification text
content.sound = !sound.isEmpty() ? [UNNotificationSound soundNamed: sound.toNSString()] : [UNNotificationSound defaultSound]; // your sound
// content.sound = [UNNotificationSound criticalSoundNamed: sound.toNSString() withAudioVolume: 1.0]; // For that you need Apple's permission
content.badge = #([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// create trigger time
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
// unique identifier
NSString* identifierNSString = identifier.toNSString();
// create notification request
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifierNSString
content:content trigger:trigger];
// add request
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = id(m_Delegate);
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(#"Local Notification failed");
}
}];
I have a code that allows the device to run an update. It works perfectly when the ipad is not under guided access for information under iOS11 (and it worked under iOS10 & guided accesss):
- (void)viewDidLoad
{
...
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:#"Update!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:#"Update!"
arguments:nil];
// Configure the trigger for a 7am update.
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 18;
date.minute = 31;
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:date repeats:NO];
// Create the request object.
UNNotificationRequest* request = [UNNotificationRequest
requestWithIdentifier:#"update" content:content trigger:trigger];
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"%#", error.localizedDescription);
}
}];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
// Update the app interface directly.
NSLog(#"");
// Play a sound.
completionHandler(UNNotificationPresentationOptionSound);
}
I found that Ticket, but no exhaustive explanations to accomplish it:
Is it possible under iOS11 to launch notification on guided access?
Thanks in advance.
It appears this was nothing more than an iOS bug which has been fixed in iOS 11.2.5 beta 4. Please see my own question here for more information.
If you can wait until iOS 11.2.5 is released (which will hopefully be soon according to Apple) the issue should just resolve itself. Otherwise you'll need to investigate something like a sockets system instead.
My application in background or inactive mode then local notification not work. I have never receive local notification on watch.
Update: less then 3 minutes schedule a local notification it's work fine but more then 3 minutes it's not work. so how to resolve this issues?
As per my understanding My code is as follows.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
// Objective-C
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = [NSString localizedUserNotificationStringForKey:#"Remider!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:#"Your watch is out of range" arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// Time
// 15 min
double timer = 15*60;
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time
repeats:NO];
// Actions
UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:#"Track"
title:#"Track" options:UNNotificationActionOptionForeground];
// Objective-C
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:#"UYLReminderCategory"
actions:#[snoozeAction] intentIdentifiers:#[]
options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories = [NSSet setWithObject:category];
// Objective-C
[center setNotificationCategories:categories];
// Objective-C
content.categoryIdentifier = #"UYLReminderCategory";
NSString *identifier = [self stringUUID];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Something went wrong: %#",error);
}
}];
Appreciate if any suggestion or idea.
Make sure your iphone is locked. When it comes to notification, its about preference where to deliver that notification.
Run your watch app on simulator, from iPhone simulator schedule the notification and lock the iPhone simulator screen, keep the watch simulator active, in that case when notification is triggered , it will be delivered on your watch simulator. Same will be the case when you will test on actual devices.
Source Link
And when both iphone and watch is locked, preference is iphone.
UPDATE
Notification on Apple Watch
I am using local notifications in my app to alert urgent messages to the user. What happens is the user receives a push notification, then a local notification is created and fired 60 seconds later with a time interval of 60 seconds. This works great and the urgent notification fires every 60 seconds as expected.
The problem is after about 20 minutes the local notification stops firing. This does not occur all the time and sometimes the notification can fire for hours with no problem. However, this problem seems to occur more often than not.
On iOS 9 we did not experience this problem at all and the notification would fire repeatedly overnight even, so I am thinking this might be something related to iOS 10?
I've also got a hunch that perhaps this has to do with memory pressure from the OS? If I attach to xCode and debug then the problem does not occur at all and will fire into infinity.
The code I use to create the notification is as follows:
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
dispatch_async(dispatch_get_main_queue(), ^{
//If notification already exists then kill it with fire
[self removeAllLocalNotifications];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = #"Urgent";
content.sound = [UNNotificationSound soundNamed:#"dingdingding.wav"];
content.categoryIdentifier = #"urgentCategoryId";
content.body = #"You have an urgent message.";
// Deliver the notification in scheduled amount of seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:#"urgentMessageId" content:content trigger:trigger];
// Schedule the notification.
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(#"Notification creation Error");
}
if (completionHandler) {
completionHandler(UIBackgroundFetchResultNewData);
}
});
}];
});
}];
Any help would be much appreciated as this is a very frustrating issue.
I'm firing a local notification. Since UILocalNotification class is deprecated in iOS 10, I have used UserNotifications.framework.
When I try to set the custom sound for the notification, the default sound is playing all time.
Here is my code:
- (IBAction)fireLocalNotification:(id)sender {
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:#"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:#"Hello_message_body"
arguments:nil];
content.sound = [UNNotificationSound soundNamed:#"sound.mp3"];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:#"FiveSecond"
content:content trigger:trigger];
// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError *error){
if(!error){
NSLog(#"Completion handler for notification");
}
}];
}
My sound is fine; sound.mp3 is present in project bundle itself.
More info:
https://developer.apple.com/reference/usernotifications/unusernotificationcenter?language=objc
Try deleting the app from the device, clean and run the app again on device.
Sometimes, resources are not properly updated; I think that is the problem in your case.
Long audio-files are not supported.
Is your file longer then 30 seconds? If yes, it will not work. Only files shorter then 30 seconds are available. If the file is longer then 30 seconds then it will be replaced with default sound.
UNNotificationSound documentation