如何在通知区域iOS上显示推送通知

时间:2015-08-18 07:17:24

标签: ios objective-c

我有Json Array推送Notification到达此类,

 {
    "alert" : "You got your emails.",

    "badge" : 9
 }

我想立即在iphone的通知栏中显示这个数组ipad

2 个答案:

答案 0 :(得分:1)

AppDelegate文件中。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSString *msg = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"MyTitle" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

这将在您的有效负载中显示您的消息。提供您的申请的时间是backgroundforegroundNOT terminated

答案 1 :(得分:0)

  

使用下面的库在导航栏中显示通知

MPNotificationView

应用内通知视图,模仿状态栏上方显示的iOS 6通知视图。

功能

  

类似于iOS 6通知的动画和布局

     

提供两个UILabel和一个UIImageView

     

简单API

     

将多个通知排入队列并使用默认值进行显示      持续时间为2秒。

enter image description here

如果你想得到通知,你必须在Appdelegate.m文件中做几个代码,如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

}

#pragma mark Notification tokan

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    deviceTokenForPushNotification  = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    deviceTokenForPushNotification = [deviceTokenForPushNotification stringByReplacingOccurrencesOfString:@" " withString:@""];


    NSUserDefaults *objUserDefalut = [NSUserDefaults standardUserDefaults];
    [objUserDefalut setValue:deviceTokenForPushNotification forKey:@"DeviceToken"];
    [objUserDefalut synchronize];

    NSLog(@"My token is: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{

    NSUserDefaults *objUserDefalut = [NSUserDefaults standardUserDefaults];
    [objUserDefalut setValue:@"" forKey:@"DeviceToken"];
    [objUserDefalut synchronize];
}