I'm new to iPhone development, but managed to receive push notifications in my iOS App. However, when I swipe away the incoming push notification, it just opens the app, but not the related post to the notification.
This is my code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"Eine Nachricht ist angekommen, während die App aktiv ist");
NSString* alert = [[userInfo objectForKey:#"aps"] objectForKey:#"id"];
NSLog(#"Nachricht: %#", alert);
//This is to inform about new messages when the app is active
//UIApplicationState state = [[UIApplication sharedApplication] applicationState];
//if (state == UIApplicationStateActive) {
// UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Neuer Artikel" message:#"Nachricht" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
// [alertView show];
// }
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"Device Token=%#", deviceToken);
NSUInteger theCount = [deviceToken length];
NSMutableString *theString = [NSMutableString stringWithCapacity:2 * theCount];
unsigned char const *theBytes = [deviceToken bytes];
for(NSUInteger i = 0; i < theCount; ++i) {
[theString appendFormat:#"%2.2x", theBytes[i]];
}
NSString* url = [NSString stringWithFormat:#"HERE_IS_MY_REGISTERING_URL",theString,theString];
NSLog(#"APNS URL : %#",url);
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) {
if (error) {
NSLog(#"Error: %#", error);
}
}];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(#"Error bei der Registrierung");
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.frame = [[UIScreen mainScreen] bounds];
[self setApplicationDefaults];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
//This is the start of the push notification settings
[self.window makeKeyAndVisible];
Now, I have no Idea what to put where, to open a related post to a push notification...
What do you expect? From your code, I can not see that you are providing any information about which post you want to be opened. Neither Apple, nor Xcode, or your code will know that by magic.
In your payload for the push notification, you must provide information what post you are referring to, and then read this information in your didReceiveRemoteNotification.
See: "Examples of JSON Payloads" here: Apple Push Notification Service
Related
The iOS app I have been hired to build uses Batch and Rover. While I have had success in getting Batch to display an incoming notification, Rover is not even attempting to connect. I've been trying to figure this problem out for a few hours now. Here is my AppDelegate:
#import Batch;
#import Rover;
#import "app_appDelegate.h"
#import "BT_loading.h"
#implementation app_appDelegate
//didFinishLaunchingWithOptions...
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[BT_debugger showIt:self message:[NSString stringWithFormat:#"didFinishLaunchingWithOptions (iOS)%#", #""]];
[Batch startWithAPIKey:#"_removed_"];
[BatchPush registerForRemoteNotifications];
[Rover setupWithApplicationToken:#"_removed_"];
[Rover registerForNotifications];
[Rover startMonitoring];
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
// notificationPayload = #{#"nickname":#"Wine List 1"};
[self appOpenDeepLink:notificationPayload];
// Register for Push Notitications, if running iOS 8
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
if (application.applicationState != UIApplicationStateBackground) {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced
// in iOS 7). In that case, we skip tracking here to avoid double
// counting the app-open.
BOOL preBackgroundPush = ![application respondsToSelector:#selector(backgroundRefreshStatus)];
BOOL oldPushHandlerOnly = ![self respondsToSelector:#selector(application:didReceiveRemoteNotification:fetchCompletionHandler: self: showDeepLinkScreen:userInfo:)];
BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
//[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
}
}
}
NSLog(#"Notification Payload: %#", notificationPayload);
//hide status bar on launch...
[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationNone];
//setup the window on launch...
[self setWindow:[[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]];
[self.window setBackgroundColor:[BT_color getColorFromHexString:#"#FFFFFF"]];
//load initial storyboard, "BT_loading"...
UIStoryboard *launchStoryboard = [UIStoryboard storyboardWithName:#"BT_loading" bundle:nil];
BT_viewController *launchViewController = [launchStoryboard instantiateViewControllerWithIdentifier:#"BT_loading"];
//set the rootViewController on the window to the BT_loading storyboard...
self.window.rootViewController = launchViewController;
[self.window makeKeyAndVisible];
//return...
return TRUE;
}
- (void) appOpenDeepLink:(NSDictionary *) notificationPayload{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"screenNickname"];
if(notificationPayload[#"nickname"]){
[[NSUserDefaults standardUserDefaults] setObject:notificationPayload[#"nickname"] forKey:#"screenNickname"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
//when app becomes active again
- (void)applicationDidBecomeActive:(UIApplication *)application{
//make sure we have an app...
if([self rootApp] == nil){
[BT_debugger showIt:self message:[NSString stringWithFormat:#"applicationDidBecomeActive rootApp not available yet (iOS).%#", #""]];
}else{
[BT_debugger showIt:self message:[NSString stringWithFormat:#"applicationDidBecomeActive (iOS)%#", #""]];
//flag as visible...
[self setUiIsVisible:TRUE];
//report to cloud (not all apps do this, dataURL and reportToCloudURL required)...
if([[self.rootApp dataURL] length] > 1 && [[self.rootApp reportToCloudURL] length] > 1){
if(![self isRefreshing]){
[self reportToCloud];
}
}
//if we have a location manager...
if([self rootLocationManager] != nil){
[self.rootLocationManager setUpdateCount:0];
[self.rootLocationManager.locationManager startUpdatingLocation];
}
}
}
//promptforPushNotificationsAfterDelay...
-(void)promptforPushNotificationsAfterDelay{
[BT_debugger showIt:self message:[NSString stringWithFormat:#"promptforPushNotificationsAfterDelay%#", #""]];
if([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
//didRegisterForRemoteNotificationsWithDeviceToken...
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
[Rover didRegisterForRemoteNotificationWithDeviceToken:deviceToken];
[BT_debugger showIt:self theMessage:[NSString stringWithFormat:#"didRegisterForRemoteNotificationsWithDeviceToken (iOS): Device Token: %#", deviceToken]];
//if we have a token, and a register it...
if([deviceToken length] > 1 && [[self.rootApp registerForPushURL] length] > 1){
//clean up token...
NSString *useToken = [NSString stringWithFormat:#"%#", deviceToken];
useToken = [useToken stringByReplacingOccurrencesOfString:#"<"withString:#""];
useToken = [useToken stringByReplacingOccurrencesOfString:#">"withString:#""];
useToken = [useToken stringByReplacingOccurrencesOfString:#" "withString:#""];
//save it for next time...
[BT_strings setPrefString:#"lastDeviceToken" valueOfPref:useToken];
//append deviceToken and deviceType to end of URL...
NSString *useURL = [[self.rootApp registerForPushURL] stringByAppendingString:[NSString stringWithFormat:#"&deviceType=%#", #"ios"]];
useURL = [useURL stringByAppendingString:[NSString stringWithFormat:#"&deviceToken=%#", useToken]];
//append currentMode ("Live" or "Design") to end of URL...
useURL = [useURL stringByAppendingString:[NSString stringWithFormat:#"¤tMode=%#", [self currentMode]]];
//merge environment variables in URL...
useURL = [BT_strings mergeBTVariablesInString:useURL];
//escape the URL...
NSString *escapedUrl = [useURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//tell the BT_device to register on the server (the device class makes the URL request)...
[appDelegate.rootDevice registerForPushNotifications:escapedUrl];
}
}
//didFailToRegisterForRemoteNotificationsWithError...
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
[BT_debugger showIt:self theMessage:[NSString stringWithFormat:#"didFailToRegisterForRemoteNotificationsWithError (iOS): ERROR: %#", error]];
}
//didReceiveRemoteNotification..
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[BT_debugger showIt:self theMessage:[NSString stringWithFormat:#"didReceiveRemoteNotification (iOS)%#", #""]];
//NSLog(#"Message ID: %#", userInfo);
//NSLog(#"%#", userInfo);
//(void)[Rover didReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
//don't do anything if the app is not in the foreground. iOS handles inbound APNS message when app is in the background...
if(application.applicationState == UIApplicationStateActive){
NSString *alertMsg;
NSString *badge;
NSString *sound;
//alert message...
if([[userInfo objectForKey:#"aps"] objectForKey:#"alert"] != NULL){
alertMsg = [[userInfo objectForKey:#"aps"] objectForKey:#"alert"];
}
//badge...
if([[userInfo objectForKey:#"aps"] objectForKey:#"badge"] != NULL){
badge = [[userInfo objectForKey:#"aps"] objectForKey:#"badge"];
}
//sound...
if([[userInfo objectForKey:#"aps"] objectForKey:#"sound"] != NULL){
sound = [[userInfo objectForKey:#"aps"] objectForKey:#"sound"];
}
//if we have a sound...
if([sound length] > 1){
[self performSelector:#selector(playSoundFromPushMessage:) withObject:sound afterDelay:.1];
}
//show messsage...
//UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"" message:alertMsg preferredStyle:UIAlertControllerStyleAlert];
//UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *alertTag)
// {
// OK button tappped.
//[self dismissViewControllerAnimated:YES completion:^{
//}];
// }];
// [alert addAction:MyAlert];
//[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:alert animated:YES completion:nil];
//show messsage...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:alertMsg delegate:nil cancelButtonTitle:NSLocalizedString(#"ok", "OK") otherButtonTitles:nil];
[alert show];
////WATCH
}//in foreground...
}
//playSoundFromPushMessage...
-(void)playSoundFromPushMessage:(NSString *)soundEffectFileName{
[BT_debugger showIt:self theMessage:[NSString stringWithFormat:#"playSoundFromPushMessage: %#", soundEffectFileName]];
NSString *theFileName = soundEffectFileName;
if([BT_fileManager doesFileExistInBundle:theFileName]){
NSURL *soundFileUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], theFileName]];
NSError *error;
AVAudioPlayer *tmpPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileUrl error:&error];
if(!error){
[tmpPlayer setNumberOfLoops:0];
[tmpPlayer prepareToPlay];
[tmpPlayer play];
}else{
[BT_debugger showIt:self theMessage:[NSString stringWithFormat:#"didReceiveRemoteNotification soundEffectPlayer ERROR: %#", [error description]]];
}
}
}
//playSoundEffect...
-(void)playSoundEffect:(NSString *)theFileName{
[BT_debugger showIt:self message:[NSString stringWithFormat:#"playSoundEffect %#", theFileName]];
if([theFileName length] > 3){
if([self.rootSoundEffectPlayer.soundEffectNames containsObject:theFileName]){
int playerIndex = (int)[self.rootSoundEffectPlayer.soundEffectNames indexOfObject:theFileName];
AVAudioPlayer *tmpPlayer = (AVAudioPlayer *)[self.rootSoundEffectPlayer.soundEffectPlayers objectAtIndex:playerIndex];
if(tmpPlayer){
[tmpPlayer play];
}
}else{
[BT_debugger showIt:self message:[NSString stringWithFormat:#"playSoundInBundle:ERROR. This sound effect is not included in the list of available sounds: %#", theFileName]];
}
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"Message ID: %#", userInfo);
NSLog(#"%#", userInfo);
(void)[Rover didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
#end
I have been going off the instruction created by the Rover developers at: https://github.com/RoverPlatform/rover-ios
I'm probably missing something simple but guidance would definitely be appreciated.
My logcat's responses to Rover:
Rover [4] - [LocationManager.swift.startMonitoring() - Line 76] => Monitoring started.
Rover [4] - [EventOperation.swift.execute() - Line 106] => Submitting event - applicationOpen(2018-02-28 07:46:02 +0000)
Rover [4] - [BluetoothStatusOperation.swift.execute() - Line 34] => Checking Bluetooth status
Rover [4] - [BluetoothStatusOperation.swift.centralManagerDidUpdateState - Line 48] => Determined Bluetooth status - true
Rover [4] - [LocationManager.swift.locationManager(_:didUpdateLocations:) - Line 114] => Received location update.
Rover [0] - [NetworkOperation.swift.execute() - Line 108] => Client error
Rover [4] - [EventOperation.swift.execute() - Line 106] => Submitting event
Rover [0] - [NetworkOperation.swift.execute() - Line 108] => Client error
2018-02-28 02:46:03.792824-0500 app[1540:560757] [CoreBluetooth] XPC connection invalid
If you put a breakpoint on the [Rover startMonitoring]; what happens?
Does rover require you to pass it your APNS token to work?
I am trying to open a url that is passed in a push notification in ios 10.
So far I haven't found a way to open a the url without opening the app.
Is there a way to open urls received in a push notification without opening the app?
I have found a work-around to open the url (the work-around works for ios <10), but then again the app opens up first.
Update:
I've noticed that for the device (iOS 10.0)
The following mentohds get involked
application:didRegisterForRemoteNotificationsWithDeviceToken
userNotificationCenter:willPresentNotification:
userNotificationCenter:didReceiveNotificationResponse:
But -application:didReceiveRemoteNotification:fetchCompletionHandler: and -application:didReceiveRemoteNotification: aren't called.
I'm new to ios and this is how far I have gotten:
AppDelegate.h
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
AppDelegate.m
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10)
{
UNUserNotificationCenter * notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
notificationCenter.delegate = self;
[notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * error)
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
if (error)
{
NSLog(#"Auth. error:%#",[error localizedDescription]);
}
}];
[notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * settings) {
}];
}
else
{
UIUserNotificationType type = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
return YES;
}
...
#pragma mark Push Notification methods
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"didReceiveRemoteNotification:");
NSString * message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
NSString * urlString = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
NSLog(#"1 Received Push URL: %#", urlString);
NSURL * url = [NSURL URLWithString:urlString];
if(url!=nil)
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10) {
// iOS 10 and above
[[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:nil];
}
else
{
[[UIApplication sharedApplication] openURL:url]; // iOS <10
}
}
if (application.applicationState == UIApplicationStateInactive)
{
NSLog(#"Application inactive");
[[NSUserDefaults standardUserDefaults] setValue:message forKey:#"Push_Message"];
}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(#"Application in background");
}
else
{
NSLog(#"Application active");
}
}
#pragma mark Notification Registration methods
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString* token = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: #"<" withString: #""]
stringByReplacingOccurrencesOfString: #">" withString: #""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"didRegisterForRemoteNotificationsWithDeviceToken:\n%#",token);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"Error:%#",[error localizedDescription]);
NSLog(#"Suggest:%#",[error localizedRecoverySuggestion]);
}
#pragma mark App Push notification methods
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(#"didRegisterUserNotificationSettings");
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"UserInfo: %#",userInfo);
NSString * messageString = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
NSLog(#"Message:%#",messageString);
NSString * messageurl = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
NSLog(#"2 Received Push URL: %#", messageurl);
NSURL * url = [NSURL URLWithString:messageurl];
if(url!=nil)
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10) {
// iOS 10 and above
[[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:nil];
}
else
{
[[UIApplication sharedApplication] openURL:url]; // iOS <10
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"PushMessage" object:self userInfo:#{#"alertString":messageString}];
}
#pragma mark UNUserNotificationCenterDelegate methods
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSLog(#"didReceiveNotificationResponse");
//--URL click--//
//Kindly suggest what can be done here?
completionHandler(UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSLog(#"willPresentNotification");
completionHandler(UNNotificationPresentationOptionAlert);
}
#end
Hopefully you can't control the behaviour of the push before launching the app. You get control over the push only after the app launched.
I have tried adding in Actionable Notifications for my Parse app, iPrayed 4 U. In the app, someone can "Pray" for you by clicking a button. Doing so runs a Cloud Code that includes a category for the APNS payload. In my AppDelegate I have:
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = #"THANKS_IDENTIFIER";
acceptAction.title = #"Say Thanks";
// Given seconds, not minutes, to run in the background
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
inviteCategory.identifier = #"TAGGED_CATEGORY";
[inviteCategory setActions:#[acceptAction]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification
completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:#"THANKS_IDENTIFIER"]) {
[self handleAcceptActionWithNotification:notification];
NSLog(#"Handled");
}
// Must be called when finished
completionHandler();
}
-(void) handleAcceptActionWithNotification:(NSDictionary *)notification {
NSLog(#"SendingThanks");
PFUser *me = [PFUser currentUser];
NSString *theirname = me[#"additional"];
NSString *myAlertString=notification[#"loc-args"];
NSLog(#"%#", notification);
[PFCloud callFunctionInBackground:#"thankYou"
withParameters:#{#"recipientId": myAlertString, #"theirName": theirname}
block:^(NSString *success, NSError *error) {
if (!error) {
// Push sent successfully
}
}];
}
So, I run to testing. When someone prays for me, I get the notification on my iPhone, drag down and there is the "Say Thanks" button. I click it, but nothing happens. Here is the kicker. Normally I would say, "Well, I messed something up, start debugging". However, I also have an Apple Watch. When I click the Say Thanks button attached to the notification on my Watch, it sends the Push, vs. doing nothing when I click the same button on my iPhone.
Any thoughts as to what is going on here?
UPDATE:
In console, when it fails, I get:
[Error]: The request timed out. (Code: 100, Version: 1.8.2)
2015-10-08 13:42:49.424 iPrayed[2231:712503] [Error]: Network connection failed. Making attempt 1 after sleeping for 1.463493 seconds.
Eventually I get a success call, but by that time it still doesn't actually send the Push. Any idea why it is happening only when tapped from iPhone and not watch?
Because you call completionHandler() before PFCloud finished. You need call completionHandler() in completionBlock
Do this.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:#"THANKS_IDENTIFIER"]) {
PFUser *me = [PFUser currentUser];
NSString *theirname = me[#"additional"];
NSString *myAlertString=notification[#"loc-args"];
[PFCloud callFunctionInBackground:#"thankYou" withParameters:#{#"recipientId": myAlertString, #"theirName": theirname} block:^(NSString *success, NSError *error) {
completionHandler();
}];
} else {
completionHandler();
}
}
Hi i have added push notification in my application and I want to view particular ViewControllers when the user tap the notification. In my app delegate m file I'm trying to get the register the device token to my server and from my server I'm using the php script to get the device token from server and I'm sending the notification.
The problem here I'm trying to view a particular view controller when the user taps on the notification its not working i have tried many different methods nothing had worked.
Here I'm view the popup like to send notification from the app when user trying to install application for the first time.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
return YES;
}
- (void) clearNotifications {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
Here I'm storing the device to token to my server.
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
const char* data = [deviceToken bytes];
NSMutableString * token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:#"%02.2hhX", data[i]];
}
NSString *urlString = [NSString stringWithFormat:#"url?token=%#",token];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSLog(#"token %#",urlString);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSLog(#"request %# ",urlRequest);
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
NSLog(#"data %#",urlData);
[self clearNotifications];
// NSLog(#"token ",sendUserToken);
}
Here I'm trying to view the particular method when user tap the notification.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
updatepoliticalViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"updatepoliticalViewController"];
[self.window.rootViewController presentViewController:ringingVC animated:YES completion:NULL];
}
My particular view controller name is updatepoliticalViewController its a navigation view controller please tell me in this above code where I'm doing wrong how to resolve this issue.
Thanks
When the app is in foreground state it call
application:didReceiveRemoteNotification:
but if it's not and the app is launched, for example, by swiping the alert in notification center
application:didFinishLaunchingWithOptions:
is called with key.
The good way is to call
application:didReceiveRemoteNotification:
from
application:didFinishLaunchingWithOptions:
I believe this should help
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
// YOUR CODE...
return YES;
}
// EXTENDED
Try get storyboard like that:
// Make sure the name match
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Create your object from mainstoryboard:
updatepoliticalViewController *ringingVC = [mainstoryboard instantiateViewControllerWithIdentifier:#"updatepoliticalViewController"];
And try set new root view controller
[self.window setRootViewController: ringingVC];
1) When application is running in background and When application is running in foreground
application:didReceiveRemoteNotification: method will called as below.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
{
//opened from a push notification when the app was on background
NSLog(#"userInfo->%#",[userInfo objectForKey:#"aps"]);
}
else if(application.applicationState == UIApplicationStateActive)
{
// a push notification when the app is running. So that you can display an alert and push in any view
NSLog(#"userInfo->%#",[userInfo objectForKey:#"aps"]);
}
}
2) When application is not launched (close) than application:didFinishedLaunchWithOptionsmethod will called.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
//opened from a push notification when the app is closed
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
NSLog(#"userInfo->%#",[userInfo objectForKey:#"aps"]);
}
}
else{
//opened app without a push notification.
}
}
I am using UrbanAirship to send push messages to my applications. my setting works both on development and production.
I need to send web url's as push message. when user opens the message I want it to redirect to the url that I added.
I added this code to my appdelegate.
`- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"userInfo:%#",[userInfo description]);
NSLog(#"alert:%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]);
NSLog(#"alert:%#",[[userInfo objectForKey:#"aps"] objectForKey:#"url"]);
}
and tried to send push like
{
"aps":
{
"alert": "take a look at this site ",
"url": "www.mysite.com"
}
}
I received the alert message but again it opened the application not the url.
Can you advice me how to send the push message with the url and make it open that url?
There is two way to do that
Open the url with a safari (not tested code):
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo
{
NSLog(#"userInfo:%#",[userInfo description]);
NSLog(#"alert:%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]);
NSLog(#"url:%#",[[userInfo objectForKey:#"aps"] objectForKey:#"url"]);
webViewController.url = [NSURL URLWithString:[[userInfo objectForKey:#"aps"] objectForKey:#"url"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSURL URLWithString:[[userInfo objectForKey:#"aps"] objectForKey:#"url"]];
}
Or you must to handle it on your app:
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo{
NSLog(#"userInfo:%#",[userInfo description]);
NSLog(#"alert:%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]);
NSLog(#"url:%#",[[userInfo objectForKey:#"aps"] objectForKey:#"url"]);
webViewController.url = [NSURL URLWithString:[[userInfo objectForKey:#"aps"] objectForKey:#"url"]];
}
And e.g in your WebViewController need the following methods
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Of course, on your WebViewController.h must be a
IBOutlet UIWebView *webView;
with full screen, or what you want...