gpt4 book ai didi

iphone - 在 iphone SDK 中设置对本地通知的操作

转载 作者:行者123 更新时间:2023-11-28 22:35:33 28 4
gpt4 key购买 nike

在我的应用程序中,我在我的第一个 View Controller 上安排了本地通知。它工作正常。要在“查看”按钮上设置操作,我的代码如下:

//AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
// did something Here when app launch by View button of notification
// added row in Db
}
return YES;
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{

// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);

app.applicationIconBadgeNumber=notif.applicationIconBadgeNumber++;
}

问题:第一次运行正常。

但是第二次,通知显示了,但是我的数据库中没有添加该行

//已编辑

我想在用户按下“查看”​​按钮时执行一些操作。

我如何识别该操作?

已编辑

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{


// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);

app.applicationIconBadgeNumber=notif.applicationIconBadgeNumber++;

UIApplicationState state = [app applicationState];
if (state == UIApplicationStateInactive) {
// Application was in the background when notification
// was delivered.

[self localNotif];

}


}

- (void) localNotif
{

[self databaseOpen];
NSString *maxIdPt = [NSString stringWithFormat:@"select Max(id) from Points"];
NSLog(@"Max id from points table %@",maxIdPt);
NSMutableArray *p1 = [[NSMutableArray alloc]init];
p1 = [[[database executeQuery:maxIdPt]mutableCopy]retain];


int p=[[[p1 objectAtIndex:0]valueForKey:@"Max(id)"]intValue];
NSLog(@"%d",p);


NSString *maxPt = [NSString stringWithFormat:@"select Points from Points where id = %d",p];
NSLog(@"Maxp %@",maxPt);
NSMutableArray * p2 = [[NSMutableArray alloc]init];
p2 =[[[database executeQuery:maxPt]mutableCopy]retain];

int k = [[[p2 objectAtIndex:0]valueForKey:@"Points"]intValue];
NSLog(@"Points %d",k);

k = k + 250;

NSLog(@"%d",k);


NSString *mtpoints = [NSString stringWithFormat:@"insert into Points(Points) values (%d)",k];
NSLog(@" lbl Points are %@",mtpoints);
NSMutableArray * m1 = [[NSMutableArray alloc]init];
m1 = [[[database executeQuery:mtpoints]mutableCopy]retain];
[database close];


}

我在 application didFinishLaunchingWithOptions 中调用的相同方法

分数第一次更新但第二次没有更新。甚至不显示通知。

最佳答案

当用户按下通知中的按钮时,可能第二次 didFinishLaunchingWithOptions 函数没有被调用,因为应用程序已经在后台运行(而不是从未运行的状态启动)状态)。

您还必须处理对

的函数调用
(void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification {
// do your db update here, too ...
}

为了涵盖您的应用已经在后台运行的情况。

编辑:

要查看应用程序是已启动还是已经在运行(= 用户在收到通知时按下“查看”​​与应用程序是否在前台运行),请检查应用程序状态:

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
// Application was in the background when notification
// was delivered.
}

归功于 Use Your Loaf - Adding Local Notifications With iOS 4 .

关于iphone - 在 iphone SDK 中设置对本地通知的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148352/

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