![](/img/trans.png)
[英]How to open specific controller when user taps on Push Notifications when App is NOT running (i.e Totally Killed) Objective-C
[英]Handling Push Notifications when App is NOT running (i.e Totally Killed)
我可以將推送通知發送到我的IOS設備。 但是,當我單擊該通知時,它只會打開該應用程序。 應用內未顯示任何消息。
我使用的代碼:
if (application.applicationState == 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
}
但是在上述代碼的幫助下無法顯示我的消息。 上面的代碼僅在我的應用處於打開狀態且處於打開狀態時才起作用,而不是僅顯示此警報。
請幫忙。
謝謝。
當應用程序被完全殺死時,獲取通知代碼
- (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"]);
//write you push handle code here
}
}
}
有關更多信息,請通過以下鏈接: 在應用終止時處理推送通知
從蘋果文檔
“當必須啟動您的應用程序以接收通知時,UIKit會在啟動選項字典中包含UIApplicationLaunchOptionsLocalNotificationKey或UIApplicationLaunchOptionsRemoteNotificationKey密鑰,該密鑰將傳遞給您的應用程序委托的application:willFinishLaunchingWithOptions:和application:didFinishLaunchingWithOptions:方法。這些密鑰的存在使您知道那里通知數據正在等待處理,並為您提供了適當配置應用程序界面的機會。不過,您無需使用這些方法來處理通知。在應用程序運行之后,UIKit會調用應用程序委托的其他方法,例如application:didReceiveLocalNotification:方法為您提供了處理通知數據的機會。調用哪種方法取決於您實現的方法以及用戶是否與消息的系統UI交互。”
因此,請檢查您的應用是否由於通知而啟動,如果顯示,則顯示對話框。
當應用未運行(或被完全終止)時處理推送通知
我正在發布此解決方案,因為它對我有用。
轉到您的AppDelegate.m文件。
步驟1:在此函數中編寫以下代碼:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
第2步:
插入此完整代碼:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo);
/**
* Dump your code here according to your requirement after receiving push
*/
if (application.applicationState == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateInactive){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
無論應用是處於活動狀態,處於非活動狀態還是已被完全殺死,整個代碼都將起作用。 它將為您提供AlertView的推送消息。
處理此類問題的最佳方法是在APN中使用深層鏈接。 這樣一來,您就可以嵌入可在應用程序中處理的數據,並將用戶定向到特定事件。
否則,您將只能使用ApplicationDidEnterForeground
程序委托中的ApplicationDidEnterForeground
方法。 只要將您的alertView代碼放在這里,您的應用程序就可以在任何時候運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.