gpt4 book ai didi

ios - 当我的应用程序在 fg 上时,我的应用程序如何发送推送通知?

转载 作者:行者123 更新时间:2023-11-28 06:48:50 25 4
gpt4 key购买 nike

我有一个 swift 应用。

我从我的服务器收到 GCM 推送通知。

在 IOS 中,我知道当我的应用程序在 bg 中时,操作系统会负责显示通知。

当我的应用程序在 fg 上并且我是显示通知的负责人时,我如何显示推送消息(通知)?

我应该使用本地推送(与远程推送相比)吗?

当应用程序在 bg 中时显示推送的代码是什么?

我知道这段代码处理向用户显示推送

但这不是通知,它只是打开一个警报

func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
// This works only if the app started the GCM service
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
// [END_EXCLUDE]
}

最佳答案

您将收到通知(在 userInfo 中),但如果应用程序在前台则不会显示。您可以做的是在应用程序处于前台时触发本地通知或使用 UIAlertView。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
application.applicationIconBadgeNumber = 0; //For resetting the badge number to zero.


if (application.applicationState == UIApplicationStateActive) //For checking if app is in foreground.
{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification received:" message:[NSString stringWithFormat:@"Received notification while app was in foreground:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}

关于ios - 当我的应用程序在 fg 上时,我的应用程序如何发送推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com