I have set up an app that is registered for remote notifications.
- (void)application:(UIApplication*)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *status = [NSString stringWithFormat:#"Notification Received:\n%#",userInfo.description];
NSLog(#"%#",status);
UIAlertView *alert=[[UIAlertView alloc]
initWithTitle:#"didReceiveRemoteNotification"
message:eventName
delegate:self
cancelButtonTitle:#"Hide"
otherButtonTitles:#"View",
nil];
alert.tag = kAlertTypeNotification;
[alert show];
[self vibrate];
if ( application.applicationState == UIApplicationStateActive ) {
// app was already in the foreground
**DO SOMETHING HERE**
}
else {
// app was just brought from background to foreground
}
}
So, when the app is not active, i get that really nifty banner alert, that looks so nice on the iPhone.
However, when the app is open, the only option I seem to have is to present a UIAlertView. This is intrusive in my opinion. Is there a way I can display the banner while application.applicationState == UIApplicationStateActive? Or, is there an open source library that implements this kind of feature?
Thanks for all your help!
I've asked this question more directly here:
Displaying a stock iOS notification banner when your app is open and in the foreground?
The short answer is that as of iOS 9, you cannot show the stock iOS banner when your app is in the foreground.
You will instead have to rely on a home-built or 3rd-part notification banner.
Please duplicate this radar requesting this feature: rdar://22313177
You can do whatever you want when the app is in the foreground. Who says you have to display a UIAlertView?
Related
I'm using Cordova and the plugin phonegap-plugin-push,
when my app is in foreground I don't see the notification alert.
What I have to do?
(I know the way to do it on Android platform (setting "forceShow": true in the method init of PushNotification) but this works only for Android)
Thank you in advance for your replies.
If u don't want to touch ios native things, then you can display your own custmize html/css Banner (like iOS push Baner) in following method :
push.on('notification', function(data) {
If(platform == "iOS" && data.additionalData.foreground){
// Show your Baner here
// U can also define call back on clicking of that banner
//Like navigation to respective page or close that banner
});
Denise,
Notification doesn't work in the foreground. It only works in the background. Do handle it in the foreground you need to implement the logic in the delegate method.
Below is the Objective C code snippet to show the notification in the foreground.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
application.applicationIconBadgeNumber = 0;
//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive)
{
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Did receive a Remote Notification" message:[NSString stringWithFormat:#"Your App name received this notification while it was running:\n%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
It will help you for your problem.
In iOS if your app is in foreground, push notification alert will not show. To achieve it, you should write code for alert in push notification receive delegate if your application is in foreground. To know application state in iOS this is the code.
+(BOOL) runningInForeground
{
UIApplicationState state = [UIApplication sharedApplication].applicationState;
return state == UIApplicationStateActive;
}
I'm actually working on an iOS application in which I need to handle notifications.
I'm currently using local notifications, and I need to redirect to different viewControllers when I click on a notification.
My problem is that for now the redirection is made every time I receive a notification and the application does not wait for me to click on it.
I tried many solutions and I always have the same problem.
So I'm wondering : Is it possible to redirect to specific views only when the user click on the notification and not when it appears ?
Thanks you in advance for your help.
You can try as below. I have also written code, which may help you.
When application is in Active state : you may show alert and ask user for redirect to different screen or not.
When application is in Inactive state : you may redirect user
directly to required screen because they coming in app via tap on
notification.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"Your Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"View", nil];
[alert show];
}
if (state == UIApplicationStateInactive)
{
// Redirect User to your required screen.
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
// Redirect User to your required screen.
}
}
You can judge the app state when you receive local notification using [[UIApplication sharedApplication] applicationState]
If the app is in active state, you can just show a UIAlertView indicating user receiving the local notification.So user click on the alertView,you can redirect the viewController.
I hope my answer may help you.
My Algorithm wishes to work this way-
If some desired event occurs at my server then I wish to send a small amount of data to every user my app (foreground or background). But, instead of notifying the user I wish my app to do some computation on data and acknowledge server.
Remember user must not be notified.
You can use the content-available key in the push notification data and application:didReceiveRemoteNotification:fetchCompletionHandler:. It could be considered a bit of a hack for your use case and if you try to use it too much you will get limited by Apples system.
You can do this on iOS 7. See the section "Fetching Small Amounts of Content Regularly" at iOS App Programming Guide.
You can send "empty" push notification (no text, no sound, no icon). This kind of notifications
is allowed and can be used for synchronization with server (to avoid the expensive repetitive polling from the app side).
If your app is not running then such a notification won't show up among springboard notifications.
If it is running then it will receive notification - you just have to make sure you don't show the empty ones (if you are also using notifications with actual contents). Your iOS code would look a bit like:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:#"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:#"alert"];
}
if (message)
{
if (![message isEqualToString:#""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: #"notification"
message: message
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
else
{
//this was an empty notification - here you can enqueue
//your server-synchronization routine - asynchronously if possible
}
}
If you don't intend to use any "real" notifications you don't even have to bother with decoding the message.
If you use ApnsPHP your message generation code would look like this:
$message = new ApnsPHP_Message($device_apns_token);
$message->setText('');
$message->setSound('');
$message->setExpiry(3600);
$push->add($message);
Is it possible to let a remote iOS5 notification show in the notification center even if my app is running, something like "I don't care about the remote notification at the moment even though I am running, let it show in the notification bar just as if I wasn't running"?
You show the UIAlertView to the user, instead of that.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"userInfo :%#",userInfo);
NSString* msg = [userInfo valueForKey:#"aps"];
if (self._VCObj.isViewLoaded && self._VCObj.view.window) {
// viewController is visible don't show.
}
else { // viewController is not visible
[[[UIAlertView alloc]initWithTitle:#"Title" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil] show];
}
}
}
Tutorial
This is not possible. The only workaround I can think of is store notification somehow in app, and then on applicationWillTerminate(or some other method) schedule same notification as a LocalNotification and made it appear after second or two. This however could annoy me as a user if I get a notification right after I close some app :)
I've integrated push notifications in my app. Users will receive push notification to join a group. When the user clicks Join, I've to handle something in the code. And so I'm implementing:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
This is working fine when the app is not running.
When the app is running, I don't see any UIAlertView. How can make my app show the push notification alert so that user can still decide whether to join or not?
I used code like this in my application delegate to mimic the notification alert when the app was active. You should implement the appropriate UIAlertViewDelegate protocol method(s) to handle what happen when the user taps either of the buttons.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = #"Close";
NSString *showTitle = #"Show";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Some title"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
[alertView release];
} else {
//Do stuff that you would do if the application was not active
}
}
For anyone might be interested, I ended up creating a custom view that looks like the system push banner on the top but adds a close button (small blue X) and an option to tap the message for custom action. It also supports the case of more than one notification arrived before the user had time to read/dismiss the old ones (With no limit to how many can pile up...)
Link to GitHub: AGPushNote
The usage is basically on-liner:
[AGPushNoteView showWithNotificationMessage:#"John Doe sent you a message!"];
And it looks like this on iOS7 (iOS6 have an iOS6 look and feel...)
You have to show the alert yourself if you want to. This is intentional behavior as documented here http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html below Listing 2-6
only this function will be invoked and you have to explicitly show the alert on that case no notification will come if app is running in which you have implement the notification.Put the break point there and handle the notification call when function called and show your customized alert there.
For showing alert view while running application you have to use
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
and by accessing the userInfo variable
The app will still receive the -application:didReceiveRemoteNotification message in your App Delegate, but you'll have to act on the message yourself (i.e. the alert isn't displayed by default).
The userInfo parameter contains an object with the key notificationType, which you can use to identify the push message.
Here is a version which support UIAlertController
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if (state == UIApplicationStateActive) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:notification.alertTitle
message:notification.alertBody
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self.navigationController presentViewController:alert animated:YES completion:nil];
}
}
** Please note my app uses self.navigationController in App Delegate, just hook on to any ViewController to present ( show ) the Alert **