As many of you may not know, when you receive a push notification on iOS, the top popup will not pop up if the App is in the foreground.

However, there are many ** * product managers will put forward similar requirements: that is, when the App is in the foreground, the same push window should pop up, but also can click, can jump to the specified page, and even this requirement involves the core function of the product.

Today, Pikacode is going to share with you a little plug-in that you wrote. It only takes 1 or 2 lines of code to do this.

Making: github.com/Yasashi/EBF…

Display push pop-ups and sounds exactly the same as the system when the App is in the foreground. Get push content and handle click events.

Support iOS 7~10 beta, support simulator and real machine run.

The effect

The actual effect is as follows:







  • It has exactly the same effect as the popover UI pushed by the system
  • Can automatically get the AppThe application name.Application icon
  • The system status bar will be hidden automatically when the window pops up and displayed automatically after the window is folded up
  • Built-in push sound
  • timeAnd belowClosing braceIs the same as the background color of the current page
  • Bring their ownClick on the event, click to get the push content and jump to the corresponding page
  • Bring their ownSlide gesturesQuickly fold up
  • Automatically pops up on the controller at the front

The installation

  1. Download andIn Xcode, Drag and drop to copy EBForeNotificationFolder to Xcode project.
  2. targets –> Build Settings –> Search other link –> Add - ObjC.




Local popup window

Call any one of the following lines of code in any method to pop up

#import "EBForeNotification.h" {... / / common popup window (sound system) [EBForeNotification handleRemoteNotification: @ {@ "aps" : @ {@ "alert" : @ "show content"}} sound id: 1312]. / / common popup window (the specified sound file) [EBForeNotification handleRemoteNotification: @ {@ "aps" : @ {@ "alert" : @ "show content"}} customSound: @ "my_sound. Wav"]. / / popup window with custom parameters (system) [EBForeNotification handleRemoteNotification: @ {@ "aps" : @ {@ "alert" : @ "show content"}, @ "key1" : @ "value1." @"key2":@"value2"} soundID:1312]; / / common popup window (the specified sound file) [EBForeNotification handleRemoteNotification: @ {@ "aps" : @ {@ "alert" : @ "show content"}, @ "key1" : @ "value1." @"key2":@"value2"} customSound:@"my_sound.wav"]; . }Copy the code

Popup window after receiving remote/local push

After receiving remote/local push, the popup window and sound of push will be displayed in the foreground automatically. Add code to appdelegate.m

//AppDelegate.m #import "EBForeNotification.h" //ios7 before - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { ... / / system sound pop-up window [EBForeNotification handleRemoteNotification: the userInfo sound id: 1312]. / / the specified sound file popup window [EBForeNotification handleRemoteNotification: the userInfo customSound: @ "my_sound. Wav"]. . } //ios7 later - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { ... / / system sound pop-up window [EBForeNotification handleRemoteNotification: the userInfo sound id: 1312]. / / the specified sound file popup window [EBForeNotification handleRemoteNotification: the userInfo customSound: @ "my_sound. Wav"]. . completionHandler(UIBackgroundFetchResultNewData); }Copy the code

Sound id parameter

IOS sound ID. The system push service uses tritone by default. The ID is 1312

Other system sound ids can be queried here: iOS Predefined Sounds Alternate address AudioServices sounds

Listen for and handle click events

Add an Observer to listen to EBBannerViewDidClick to get the push content and handle its own logic through the user-defined fields during push, such as jump to the corresponding page, etc.

The received push content is similar to the following:

{" aps ": {" alert" : "tweets", "sound" : "sound", "badge" : "3"}, "key1" : "jump page 1" / / custom} this field to jump to the corresponding pageCopy the code

Add an Observer to get a custom field and handle:

#import "EBForeNotification.h" [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eBBannerViewDidClick:) name:EBBannerViewDidClick object:nil]; -(void)eBBannerViewDidClick:(NSNotification*)noti{if(noti[@key1 == @key1]){}Copy the code