Handling Push Notification on Lock Screen ios - ios

In my application Push notification receives data in json format
Which is this
aps = {
alert = "{\"messsage\":\"what to do when boarded \",\"chatBox\":\"130701.130693\",\"sender_id\":\"130701\",\"sender_name\":\"reg41\",\"sender_image_url\":\"http:\\/\\/www.playmit.com\\/images\\/user_profile_images\\/\",\"receiver_id\":\"130693\",\"type\":\"chat\"}";
};
}
But also on lock screen when app is not running or app is in background when push notification received it shows same json contents in push notification
So how do I handle this.
Thanks.
In the image Quizmatch receives push notification in json format

You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
So in your case you should do something like:
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}
Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":
NSLog(#"%#",notification['Person']['Address']);
Above will output:
this is a test address
You could find more about custom payloads, along with some examples in Apple docs.
Reference:link

if user is not prevented from settings, aps.alert is always displayed
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
},
"job_id": 1
}
update your notification structure like this, send data under some other key like "job_id"

Related

FCM Push Notification "content" entry

I'm using FCM to publish push notifications to my users, which is working great so far.
Currently I'm trying to implement a Notification Content Extension to deliver customized push notification and previews, which is working great with local notifications, following this post.
As far I know, I have to set the category entry and my notification category identifier in the push notification, in order to tell iOS, which notification UI it is supposed to use.
The problem is, when I send the following message to FCM, with the category entry set, FCM erases the entry or changes it to gcm.notification.category, depending where I place the category entry (aps / data, etc.)
This way iOS never shows my custom UI / extension. Unfortunately I was not able to find any help in the FCM documentation.
Send (POST: https://fcm.googleapis.com/fcm/send):
{
"notification": {
"title": "Good Morning",
"body": "Wake up Jack!",
"badge" : 1,
"sound" : "horn.aiff",
"category" : "Cheers" <-- Is going to be deleted / changed
},
"data" : {
"time" : "2018-01-19 23:00:00",
...
},
"mutable_content" : true,
"priority" : "high",
"registration_ids" : [
"abcdefg123456"
]
}
Received:
{
aps = {
alert = {
body = "Wake up Jack!";
title = "Good Morning";
};
badge = 1;
"mutable-content" = 1;
};
"gcm.message_id" = "0:1516392279506894%dc84760ddc84760d";
"gcm.notification.category" = "Cheers"; <-- not working
}
The category APNS parameter FCM counterpart is click_action.
When adding in a custom parameter (using the data message payload), it is handled differently for iOS and is often included outside of the aps payload (like in the sample you provided).

IOS Access Push notification payload before user gets push notif

I'm trying to access the push notification payload before user actually gets the push notification. I want to create some sort of filter of the push notifications that the user receives. Is this possible?
You can get notification payload before it fired using this extension "NotificationService"
Add one target in the project and you can get notification data in bellow function.
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
}
One more thing you need to add is in notification payload add two-parameter like this
{
"aps":{
"alert": {
"title": "This it title",
"body": "This is body"
},
"mutable-content":1,
"content-available":1
},
"mediaType": "png",
"mediaUrl": "your url her"
}
"mutable-content":1,
"content-available":1
mutable content and content available = 1
if you do not add these two keys in payload then the above function should not call.
Thank you.

Apple watch real response data

Can you print and show me the output of this with real device I need to know the format of it.
- (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler
{
// This method is called when a remote notification needs to be presented.
// Implement it if you use a dynamic notification interface.
// Populate your dynamic notification interface as quickly as possible.
// After populating your dynamic notification interface call the completion block.
NSLog(#"%#",remoteNotification);
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
This is the Payload response for the question
{
"aps": {
"alert": {
"body": "Test message",
"title": "Optional title"
},
"category": "myCategory"
},
"WatchKit Simulator Actions": [
{
"title": "First Button",
"identifier": "firstButtonAction"
}
],
"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."
}

Apple Watch Notification Payload

I want to add values to the apple watch notification (the current screen uses hard coded data):
The values I want to add are for these fields: "Amount", "At" and "When". How can I add get this values from the PushNotificationPayload.apns file and show it on the notification?
This is the PushNotificationPayload.apns file:
{
"aps": {
"alert": {
"body": "New Transaction\n\n",
"title": "Optional title"
},
"category": "newTransactionCategory"
},
"WatchKit Simulator Actions": [
{
"title": "Details",
"identifier": "transactionDetailsButtonAction"
}
],
"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."
}
These are the steps,
Create a new class which is a subclass of WKUserNotificationInterfaceController.
From the storyboard, select your Dynamic Interface Scene for notification (if you have not created it, enable 'Has Dynamic Interface' in attribute inspector of your Static Scene ) and in Identity Inspector set custom class as created above.
Now modify your PushNotificationPayload.apns file contents as below,
{
"aps": {
"alert": {
"body": "New Transaction\n\n",
"title": "Optional title"
},
"category": "newTransactionCategory"
},
"WatchKit Simulator Actions": [
{
"title": "Details",
"identifier": "transactionDetailsButtonAction"
}
],
"Amount": "USD 20",
"At": "Mc Donalds",
"When": "Today",
}
When a remote notification is received, this method will be called in your custom notification interface class and you will receive the custom keys in the dictionary 'remoteNotification' which you need to use to set the text of your labels here.
-(void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {
NSLog(#"remoteNotification Dictionary %#",remoteNotification);
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
Last is debugging:
Select your targets on the top and select Edit Scheme
Click on duplicate scheme at the bottom and give your custom name like 'NOTIFICATION-Mywatchkitapp' etc...
Then, select WatchKit Interface to Dynamic Notification, Notification Payload to your PushNotificationPayload.apns file and run for this target.

How we can fetch push notification in background

I am receiving push notification in that payload message i am receiving one URL with message as push notification. but i dont want to show URL to user i want to show only message to user. is it possible from ios side.
If your are using url as a separate key in aps then its possible you can display alert only as a message, otherwise any message can't be modify message in background.
"aps": {
"alert": "alert!",
"sound": "default",
"URL" : "your url"
}
yes, its possible but it also depend on how you have created your payload but its simple as you create your payload with your link and message, receive at your delegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushChatObject {
// get the message value and NSLog is or set to UIAlert or in NSString
(pushChatObject)[#"Message"];
}
You can, but you shouldn't, because
Delivery of notifications is a “best effort”, not guaranteed. It is
not intended to deliver data to your app, only to notify the user that
there is new data available.
(c) Apple
Specify the notification message as
{
"aps": {
"alert": "alert!",
"sound": "default"
},
"URL": "http://apple.com"
}
When you receive the notification in the app just check for your param in the notification dictionary:
// Place this method to AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:
(NSDictionary *)notification {
if ([notification objectForKey:#"URL"]) {
NSString *url = [[notification objectForKey:#"URL"] stringValue];
}
}
Check this section of Apple's Local and Push Notification Programming Guide for more info
I think in this case you can use Child properties of the alert property, you use 1 argument as a alert and another argument as a url, like:
"aps": {
"alert" : {
"loc-key" : "ALERT",
"loc-args" : [ "Your alert message", "Your url"]
},
"sound": "default"
}
When the device receives the notification, it uses "ALERT" as a key to look up the associated string value in the Localizable.strings file in the .lproj directory for the current language. Assuming the current localization has an Localizable.strings entry such as this: "ALERT" = "%#";
And later on you can get your url using aps NSDictionary.

Resources