ios - Calling didReceiveRemoteNotification when app is launching for the first time -
i have implemented didreceiveremotenotification method. works , displays view controller notification info passed through. works when app in foreground or if running in background. however, when app not running , user clicks notification, app launches, appears if no notification has been received. notification not written text file , viewcontroller not beingness pushed.
- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo { if ( application.applicationstate == uiapplicationstateactive) { nsdictionary *apsinfo = [userinfo objectforkey:@"aps"]; nsstring *alertmsg = @""; nsstring *badge = @""; nsstring *sound = @""; nsstring *custom = @""; if( [apsinfo objectforkey:@"alert"] != null) { alertmsg = [apsinfo objectforkey:@"alert"]; } if( [apsinfo objectforkey:@"badge"] != null) { badge = [apsinfo objectforkey:@"badge"]; } if( [apsinfo objectforkey:@"sound"] != null) { sound = [apsinfo objectforkey:@"sound"]; } if( [userinfo objectforkey:@"type"] != null) { custom = [userinfo objectforkey:@"type"]; } // set appending text. nsstring *texttoadd = [nsstring stringwithformat:@":%@", alertmsg]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filename = [nsstring stringwithformat:@"%@/accountnotifications.txt", documentsdirectory]; nsstring *filecontents = [[nsstring alloc] initwithcontentsoffile:filename usedencoding:nil error:nil]; nsstring *texttofile; if (filecontents == null) { texttofile = alertmsg; } // here append new text existing 1 if (filecontents != null) { texttofile = [filecontents stringbyappendingstring:texttoadd]; } // here save updated text file paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); documentsdirectory = [paths objectatindex:0]; filename = [nsstring stringwithformat:@"%@/accountnotifications.txt", documentsdirectory]; nsstring *content = texttofile; [content writetofile:filename atomically:no encoding:nsstringencodingconversionallowlossy error:nil]; nsarray *filedata = [texttofile componentsseparatedbystring:@":"]; nsmutablearray *tabledatafromfile; tabledatafromfile = [[nsmutablearray alloc] init]; int = 0; (i = 1; < [filedata count]; i++) { [tabledatafromfile addobject:filedata[i]]; } notificationviewcontroller *vc = [[notificationviewcontroller alloc] initwithnibname:@"notificationviewcontroller" bundle:nil]; vc.tabledata = tabledatafromfile; uiviewcontroller *root = self.mainnavcontroller.topviewcontroller; nsarray *vcs = [nsarray arraywithobjects:root, vc, nil]; [self.mainnavcontroller setviewcontrollers:vcs animated:yes]; } // app in foreground else { while (done == false) { } nsdictionary *apsinfo = [userinfo objectforkey:@"aps"]; nsstring *alertmsg = @""; nsstring *badge = @""; nsstring *sound = @""; nsstring *custom = @""; if( [apsinfo objectforkey:@"alert"] != null) { alertmsg = [apsinfo objectforkey:@"alert"]; } if( [apsinfo objectforkey:@"badge"] != null) { badge = [apsinfo objectforkey:@"badge"]; } if( [apsinfo objectforkey:@"sound"] != null) { sound = [apsinfo objectforkey:@"sound"]; } if( [userinfo objectforkey:@"type"] != null) { custom = [userinfo objectforkey:@"type"]; } // set appending text. nsstring *texttoadd = [nsstring stringwithformat:@":%@", alertmsg]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filename = [nsstring stringwithformat:@"%@/accountnotifications.txt", documentsdirectory]; nsstring *filecontents = [[nsstring alloc] initwithcontentsoffile:filename usedencoding:nil error:nil]; nsstring *texttofile; if (filecontents == null) { texttofile = alertmsg; } // here append new text existing 1 if (filecontents != null) { texttofile = [filecontents stringbyappendingstring:texttoadd]; } // here save updated text file paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); documentsdirectory = [paths objectatindex:0]; filename = [nsstring stringwithformat:@"%@/accountnotifications.txt", documentsdirectory]; nsstring *content = texttofile; [content writetofile:filename atomically:no encoding:nsstringencodingconversionallowlossy error:nil]; nsarray *filedata = [texttofile componentsseparatedbystring:@":"]; nsmutablearray *tabledatafromfile; tabledatafromfile = [[nsmutablearray alloc] init]; int = 0; (i = 1; < [filedata count]; i++) { [tabledatafromfile addobject:filedata[i]]; } notificationviewcontroller *vc = [[notificationviewcontroller alloc] initwithnibname:@"notificationviewcontroller" bundle:nil]; vc.tabledata = tabledatafromfile; uiviewcontroller *root = self.mainnavcontroller.topviewcontroller; nsarray *vcs = [nsarray arraywithobjects:root, vc, nil]; [self.mainnavcontroller setviewcontrollers:vcs animated:yes]; } // app brought background foreground }
could please help me solve problem? boolean done set true 1 time didfinishlaunchingwithoptions completed. want notificationviewcontroller open , display notification if notification pressed while app not running @ all.
you should add together code :
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { nsdictionary *remotenotif = [launchoptions objectforkey: uiapplicationlaunchoptionsremotenotificationkey]; //accept force notification when app not open if (remotenotif) { [self handleremotenotification:application userinfo:remotenotif]; homecoming yes; } homecoming yes; }
you can move logic didreceiveremotenotification
mutual function , phone call function both places. work if user open app tapping notification. if user opens app tapping app icon, notification info won't reach app.
ios objective-c xcode push-notification apple-push-notifications
No comments:
Post a Comment