Retrieving a deeplink in a notification received while the app was closed - ios

I recently found out that, when my app was closed (I'm talking about it being completely closed, not in the background), any push with deeplink that I open will start the app, but that's it, the deeplink is never taken into account.
After investigating, I realized that when the app is closed and you open it via a push, application:didReceiveRemoteNotification is not called. It is up to the dev to check, in application:didFinishLaunchingWithOptions, the launchOptions dictionary, and look for the value of UIApplicationLaunchOptionsRemoteNotificationKey, meaning that the app was opened via a push.
Here's the description of launchOptions when opening my app in such a way :
Printing description of launchOptions:
{
UIApplicationLaunchOptionsRemoteNotificationKey = {
aps = {
alert = {
"action-loc-key" = "_push-title_";
body = "_push-budy_";
};
"content-available" = 1;
};
azme = {
au = "_deeplink-url_";
ci = "a-1";
dt = b;
pid = 5130;
};
};
}
So, not knowing any other way, I have to check for the presence of a value for UIApplicationLaunchOptionsRemoteNotificationKey in the launchOptions, then get the value for the key "azme", and the value for the key "au", which seems like a totally wrong way to do it, and the open the URL as I normally do.
I could not find a better/more conventional way to do this, is there one ?
Thanks for the help !

Related

local notification is not displayed

I'm working on a Xamarion iOS app. I want to display a local notification when an event occurs in my app, Like Outlook does when an email is received.
I'm using the following code, after having received the right to send notification from the user, of course:
var content = new UNMutableNotificationContent();
if (!string.IsNullOrWhiteSpace(value: mySoundFile))
{
content.Sound = UNNotificationSound.GetCriticalSound(mySoundFile);
}
if (!string.IsNullOrEmpty(myChannelDescription))
{
content.ThreadIdentifier = myChannelDescription;
}
content.Title = "MyTitle";
content.Body = "MyText";
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false);
var request = UNNotificationRequest.FromIdentifier(notification.Id.ToString(), content, trigger);
var center = UNUserNotificationCenter.Current;
center.AddNotificationRequest(request, null);
But the notification is not displayed.
Any help appreciated.
The reason for not displaying the notification could be several things. Try the following solutions:
Make sure that the application has the required rights to send notifications and that the user has enabled them.
Check if the sound file is valid and located in the application package. It may also be necessary to add it to the Info.plist file.
Check if the thread identifier is valid and not repeatable.
Check if the trigger time is valid. Setting 0.1 seconds is very short and may not be enough to display the notification.
If the above solutions do not work, it is worth using debugging tools to more closely examine why the notification is not displayed.
On iOS, you must request permission to use notifications before attempting to schedule them. Just like this, you can try to check if the following code is added to your project:
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
{
...
});
You can change your AddNotificationRequest as follows to see if there is an error in the notification:
center.AddNotificationRequest(request, (err) =>
{
if (err != null)
{
throw new Exception($"Failed to schedule notification: {err}");
}
});
For more details, you can refer to the following documents to check some permission issues:
Enhanced User Notifications in Xamarin.iOS | Microsoft
Asking permission to use notifications | Apple Developer
UpDate: If your app is in the foreground. You could try implementing the delegate userNotificationCenter(_:willPresent:withCompletionHandler:) which will be called when a notification arrives while the app is in the foreground. Refer to the following code:
UNUserNotificationCenter.Current.Delegate = new TestDelegate();
public class TestDelegate: UNUserNotificationCenterDelegate
{
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Alert);
}
}

ios 13 push-notification gets cleared as soon as we get when device is locked

when I'm receiving push notification on iOS 13, and device is locked. I' getting pushnotifcation, but it gets cleared immediately without any user interaction.
I'm using .net and following is payload code which I'm using
{to = notification.DeviceToken,
priority = "high",
content_available = true,
notification = new
{
body = notification.Message, //This will be a subtitle..
title = notification.Name, //This will be a title..
},
data = new
{
notificationId = notification.NotificationId,
userId = notification.UserId,
encryptedNotification = Crypto.Encrypt("id", notification.NotificationId.ToString())
}
};
string postbody = JsonConvert.SerializeObject(payload).ToString();
Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
tRequest.ContentLength = byteArray.Length;
**tRequest.Headers.Add("apns-push-type", "alert");**
**tRequest.Headers.Add("apns-priority", "5");**
}```
Please guide me what wrong in code.
have referred following azure code to implement, but not able to find the cause.
https://learn.microsoft.com/en-us/azure/notification-hubs/push-notification-updates-ios-13
This can happen if you have a code that decreases the app icon's notification badge number. If you do that, make sure you do it only if the application is active. If it is inactive and you decrease the badge number, it might cause the notification to appear and immediately disappear.

how to get additional data sent from one signal console in my iOS app?

I know that maybe similar thread has been asked in here How posting One Signal notification's additional data and receiving that? , but as a beginner I am confused how to implement the answer.
so, I try to send push notification through one signal console. and I need to send custom data in order to navigate the user to certain view controller after the user tap the push notification like this
but unfortunately I don't know how to get that key value pairs of that destination and idItem. I assume I have to do something on my AppDelegate , but I don't know how to get that key value pairs when the user interact with the push notification sent.
from that thread, I get
let PATH = notification!.payload.additionalData["PATH"]
print("PATH: ",PATH as Any)
but.... I don't know where to implement this code
I finally find the answer. in AppDelegate in didFinishLaunchingWithOptions
OneSignal.initWithLaunchOptions(launchOptions, appId: "YOUR ONE SIGNAL APP ID HERE", handleNotificationAction: { (result) in
let payload = result?.notification.payload
if let additionalData = payload?.additionalData {
let destination = additionalData["destination"] as? String ?? ""
print("the destination is: \(destination)")
}
},settings: onesignalInitSettings)

Send push notification to a specific test device using OneSignal push notification service

I added push notification support to my iOS app using OneSignal some time before. The app is made in Xcode with Swift.
I want to send a test push notification only to my test device(s). I the documentation I found the following manual: How do I send a notification to a single user?
I managed to create the segment but I don't know where to put this peace of code: OneSignal.sendTag("is_test", "true")
Does anybody know where I have to put this piece of code to make it working as I described above?
I uploaded my code here: https://codeshare.io/DxcNn
Thanks,
David.
Update:
OneSignal now also supports to set a device as test device without doing something in the code. You can also download your own app from App Store and use it as test device. Just select you device from devices list one OneSignal and mark it as test device. You can find your device in the list by model, version and/or time added.
The sendTag method is from the device sdk. In your case iOS.
https://documentation.onesignal.com/docs/ios-native-sdk#section--sendtag-
You should do this anytime after initWithLaunchOptions in the app delegate. Updated code based on comments
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
let oneSignal = OneSignal(launchOptions: launchOptions, appId: "here_is_my_onesignal_app_id") { (message, additionalData, isActive) in
NSLog("OneSignal Notification opened:\nMessage: %#", message)
if additionalData != nil {
NSLog("additionalData: %#", additionalData)
// Check for and read any custom values you added to the notification
// This done with the "Additonal Data" section the dashbaord.
// OR setting the 'data' field on our REST API.
if let customKey = additionalData["customKey"] as! String? {
NSLog("customKey: %#", customKey)
}
}
}
OneSignal.defaultClient().sendTag("is_test", value: "true")
// Override point for customization after application launch.
return true
}

APN custom notification sound issue

I have remote notifications setup and working fine. I however have to play a custom sound when a notification is received (when app is NOT in foreground). I have added a file called customSound.wav to my app bundle and made sure "add to targets" is selected when i drag and drop it into my project (is this the correct way?). Server-side: the same file name has been specified as "sound": "customSound.wav". In my didReceiveRemoteNotification i am printing the userInfo dictionary and it is coming up like this:
aps =
{
alert = "Good Evening Sir";
badge = 1;
"custom_filed1" = 1;
"custom_field2" = "AAA-BBB-CCC";
name = "Sir Hubert";
sound = default;
};
As you can see, sound is still "default". I am aware of the fact that if the sound specified can not be found, the default sound is played- will the sound value in the dictionary also be "default" or it should be the file name that was specified in payload json. Is the order in which these are specified in the payload important. I have tried suggestions from other threads but not working for me. I can not show the json as I do not have access to the system at the moment. Any suggestions as to what I may be doing wrong ?
No your sound key should have the sound file name you want to play.
{
aps =
{
alert = "message";
sound = "sound file name.extension";
badge = 1;
};
}
eg:
{
aps =
{
alert = "message";
sound = "tone.caf";
};
}
I am using this and getting successful custom alert on my notification;
keep sound duration as small as possible. I did with this file format: aiff, caf
if u want to test notification then u can try this app https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12
I am not doing any marking of this app but it is good for testing push notification

Resources