How to change notification message in ios by objective c code? - ios

am new to ios. If we receive a notification message as "Message: #"Welcome"" then i have to show only "Welcome" in the notification of ios device. Is it possible and how to do it ?
Thanks in advance for any help.

Whatever is sent in the payload of the push notification is displayed in the banner shown to the user. If you want to customize this message, you'll have to change what you are sending to the APNs service. See the push notification programming guide for details: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1

At iOS (client) side you can only program what action to be taken when user clicks on notification or when user gets remote notification . For that you have to add code in the -
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
But you will not have any control over the notification shown while your application is in background . For this you have to do string operations at server side from where you are sending the push .

You can do this while your app is in Foreground State using remove Some specific Characters. follow (how to remove first 3 characters from nsstring?) for reference.
But if you are in Background state you can not handle payload. so whatever text was send from APNs service. it will show in iOS.
Suggestion : you can send payload like below
aps = {
alert =
{
body = Welcome;
type = Message;
};
};
So you will know the type of payload and it will show only "Welcome".

Related

How handle silent push notification when application is InActive state in ios 9?

I have implement silent push notification.So "didReceiveRemoteNotification" method called when application is inactive state in ios 9.
There are some case when application is inactive state.
1.When user tab on particular notification.
2.When call or message receive.
3.When notification center and control center open.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive) //Inactive state
{
[self RedirectScreenBasedOnNotification:self.userInfoDic];//Screen Redirection code
}
}
So how can i handle silent notification when app is inactive state?
I have face problem is when notification center open at that time if any notification come then redirection will do,but i want to stop that.
Notification payload:-
aps = {
alert = "Test Dev 5 startd following you ";
"content-available" = 1;
"link_url" = "https://raywenderlich.com";
message = {
friend = {
email = "abc#gmail.com";
name = "Test Dev 5";
photo = "";
"user_id" = 27;
};
id = 3;
"is_business_sent" = 0;
message = "Test Dev 5 startd following you ";
};
sound = default;
}
Thanks in advance
Silent push notifications do not trigger user interactions. When a silent notification payload includes keys for user interaction things go wrong - iOS can't reason about wether the intent is to present something to the user, or to keep the notification silent and handled without user interaction. Sometimes the silent notification may work, other times it may be presented like a normal notification with user interaction. It can be one or the other, not both.
If the silent push key content-available is present in the aps payload the keys alert, sound, or badge should not be.
You can use my Push Notification Payload Validation Tool to check the content of your notification. The payload you posted in your question has several problems - the aps key should only contain Apple keys defined in Generating Push Notifications. All of your custom keys and values should be outside the aps object.
application:didReceiveRemoteNotification:fetchCompletionHandler: will only be called for silent push notifications. If the notification payload contains both content-available and one or more of alert, sound, or badge iOS will not know which method to call and you may see inconsistent behavior.
If you are just trying to show a non-silent notification you do not need to implement application:didReceiveRemoteNotification:fetchCompletionHandler:. Instead implement application:didReceiveRemoteNotification: for iOS 9 and userNotificationCenter:willPresentNotification:withCompletionHandler: for iOS 10 and later.
As far as silent notifications and the inactive application state, there is nothing special to be done here. Silent notifications are intended to 'hint' to the application that it should refresh content. When a silent notification is received the application is required to process the content update within 30 seconds and then call the fetch completion handler. When iOS executes the fetch completion handler it take a new restoration snapshot of the updated UI. This happens even when the application is inactive.
You can add your code in this If condition.
if (UIApplication.sharedApplication.applicationState != UIApplicationStateInactive) {
//Write your code her, this will get executed when your app is not in Inactive state.
}

Rewriting Push Notification Content in iOS

I'm developing a chat application, i want to rewrite Push Notification Content (alert) in iOS.
Suppose the alert came is: +911234567890 : Hi how are you
In this alert +911234567890 is the contact number in my phone named My Tester, i just want to rewrite the push notification to: My Tester : Hi how are you
How it can be implemented
This needs to work if the app is not running also.
May sample code is below for process push:
(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler
In push notification you can display those content that is received by the push notification. If you want display name with push notification then you have to send it from server.Even you can show in Whats App, Whats App notification doesn't show actual name we have store in phone book.It only show the name which is send by the server

Xamarin.Forms iOS Can't Customize Notifications

I've been following the tutorial found at: https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-xamarin-forms-get-started-push/ to try and get push notifications to work in my Xamarin.Forms app.
I've got them working on Android, but I'm having a bug on iOS - I need to customize the text (from the phone) before the notification is actually made, but I can't on ios since when the app is running in the background, ReceivedRemoteNotification isn't being called.
Here's something similar to what my notification handling code looks like:
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
NSObject inAppMessage;
bool success = userInfo.TryGetValue(new NSString("inAppMessage"), out inAppMessage);
if (success)
{
//change the text that is displayed
string newNotText = ModifyNotification(inAppMessage.ToString());
var alert = new UIAlertView("Got push notification", newNotText, null, "OK", null);
alert.Show();
}
}
How can I customize a notification that is received on iOS?
iOS Push and GCM is different in how they work, GCM lets the App handle the notification and start the local notification, iOS does not.
iOS only notifies your App that the notification existed, but there are a workarounds for this.
On iOS you can use silent notifications which are not seen by the user but you'll get the ReceivedRemoteNotification callback
You can read more here: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
This documentation tells you the following:
The content-available property with a value of 1 lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
So if your notification contains the "content-available" with value 1 it will be silent and you can start your own local notification after this.
Be prepared that this is not reliable in any way and if you are not a special privileged app(like VOIP) you are not able to do what you want in a reliable way on iOS
Backend example:
Just change the template var like in your tutorial used:
const string template = "{\"aps\":{\"content-available\":\"1\",\"alert\":\"$(message)\"}}";
Because it is not clear enough, if you don't want to get any notification you should not use alert or sound properties for your notification
const string template = "{\"aps\":{\"content-available\":\"1\",\"someproperty\":\"propertyvalue\"}}";

Push notification on iOS devices

I’m able to send and receive the push notification…In the push notification i will be sending some json encoded data…
Once my device receives the push notification,then if the user taps on the notification banner device can get the json data received by the push notification.…But what if user clears the notification banner,how can the iOS device receive the json data?…Any suggestion guys?…
You can set the content-available flag to 1 and iOS will call your app's delegate.
There are just 2 ways to get push notification details, when application is not working. First one is not very reliable since it will provide you latest push notification only. Second one, you can't really use if you want banner and you want to notify user. Third one is just an alternate way to get latest updates from server.
Get details of last push notification in application Launch method :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dicAPNS = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
Use silent Push notification, where user wont get any notification but you will be able to get notified. This is done by sending "content-available:1" in APNS Payload.
Call an server call to check if you have any update. So you can grab data from server what you might have received in missed push notification.
I think iOS push notification fully depends on user's wish.I think the another web service needs to load all the content of data in push notification, if push notification is disabled or deleted by user.

Silently send push notifications from my server to my app

Setup: I have successfully deployed a push notification system on my server! That being said, I am able to send notifications to my iOS devices which can send alert messages when something happens on the webserver, for example: when a new customer is added to the system.
This displays a nice message like so
You have a new customer. More money baby!
And the notification is of course silent when my application is open but simply automatically updates the customers table for me so I don't have to manually keep refreshing my table.
Problem: Since then the system has scaled up and now I'm getting messages like that to my device all the time at least a few every hour, this bothers me.
My proposed solution is to only update my table every single time a new user is added so this would require a push notification to be sent for every new user that is created, thats perfect, but for it to not display messages on my notification center like it did before.
Question: Is there a way to silently send push notification so that I don't get any messages like that on my device, but when the application is running for the push notification to still pop through so that I can update the UI without having to manually refresh my customer's table?
Is it just a case of leaving blank body parameter of the json push notification sent from my server?
What I don't want: I don't want to resolve to having to hide the notification from my own application :')
You could use the silent push notifications of iOS 7+.
If into the payload that you send from your server you add the key "content-available" with value 1 and remove the key "alert", you will get the push but with out get the message on your screen.
When you receive this kind of push you will have 30 seconds of execution time*, time that could be enough to refresh your user table and next time you open the app you will have up to date the table data.
The system will wake up your app and call the method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
on the AppDelegate.m
The format payload should be that:
{"aps":{"content-available":1}}
also you could add more fields if you want like badge or extra dictionary data to use it, are optionals.
{"aps":{"content-available":1, "sound":"", "badge":1}}
If for some reason you are not getting the push try adding "sound": "", seems like could be a bug around the silent push.
*Don't forget that execution time due to silent push won't happen if the you or the use swipe up the app and terminate it.
I hope this is useful for you! :)
According to the "Local and Push Notification Programming Guide", the aps dictionary contains one or more properties that specify the following actions:
An alert message to display to the user
A number to badge the application icon with
A sound to play
So you could send a notification that contains a badge count only. The payload would
look just like
{
"aps" : {
"badge" : 9
}
}
here it the format
/*
aps =
{
Name = "";
alert = "Testing";
price = 0;
//sound = default;//do not send it from server
type = add;
};
*/
in swift get remote notification when app is open
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
if let aps = userInfo["aps"] as? NSDictionary {
var message = aps["alert"]
println("my messages : \(message)")
}
}

Resources