Show the content of push notification in UIAlertVi

2019-08-27 18:36发布

问题:

Hi Im currently developing an app where i have push notifications activated. I use parse.com. I have got it working so far that i can send a notification and the device receives it and i also get a badge on the app. And i have set an AlertView when you enter the app from the notification. But i dont know how to display the text of the push notification in the UIAlertView. And i also want the badge to disappear when you've viewed the message. Here is the code Im using:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateInactive)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Text" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

    [alert show];
}
else if (application.applicationState == UIApplicationStateActive)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Text" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

    [alert show];
}
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

{
    NSDictionary * pushDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (pushDictionary)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Text" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

        [alert show];
    }
}

回答1:

To handle the Push Notification and show an Alert View with its info try in appDelegate:

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {     
   [PFPush handlePush:userInfo];  
}