iOS click the push message to jump processing

  • 2020-06-01 11:01:46
  • OfStack

When the user clicks the notification message to enter the application

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


There will be the userInfo information of the push message in ". "at this time, we can pass it

NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

Get the push content. If remoteNotification is not empty, that means that the user entered via a push message, then one property can be declared

@property (nonatomic) BOOL isLaunchedByNotification; 

Used to identify whether the user enters the application by clicking the notification message. At this point,

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo 

1 is bound to be called, and iOS7 is available

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 

Since MainViewController has already been initialized when this method is called, we can register the push message listener at MainViewController to display the corresponding view, as follows:

// Subscribing to the display view message opens a branch view directly 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentView:) name:@"PresentView" object:nil];// A message box pops up to prompt the user for a subscription notification message. It is mainly used for users to pop up the prompt box when using the application 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNotification:) name:@"Notification" object:nil]; 

Therefore, in AppDelegate's didReceiveRemoteNotification, different presentation methods can be notified by judging isLaunchedByNotification.

Imagine playing the code that vibrates without stopping when a push comes (not playing music).

The header file is included first

#import <AudioToolbox/AudioToolbox.h> 

Register 1 sound (use default 1007 in this example)

@property (nonatomic, assign) SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:nil];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &_soundID);
AudioServicesAddSystemSoundCompletion(_soundID, NULL, NULL, soundCompleteCallback, NULL); //  The core code   Repeatable execution 
// block  Used for  AudioServicesAddSystemSoundCompletion(_soundID, NULL, NULL, soundCompleteCallback, NULL);  A function call 
void soundCompleteCallback(SystemSoundID soundID,void * clientData)
//  Stop playing 

The above content is iOS which this site introduces to you. I hope it can help you!

Related articles: