Record how you implement local push (Localnotification), and playback sound, vibration implementation

Source: Internet
Author: User
write the following code when you need to launch a notification
BOOL isappactivity = [[UIApplication sharedapplication] applicationstate] = = uiapplicationstateactive;
    if (isappactivity) {
        //Here is the current app in the foreground, do something you want to do at this time
        return;
    }
    Uilocalnotification *noti = [[Uilocalnotification alloc] init];
    Noti.firedate = [NSDate date];
    Noti.alertbody = message?message:@ "update!";
    Use the system default push notification reminder
    noti.soundname = uilocalnotificationdefaultsoundname;
    Noti.applicationiconbadgenumber + = 1;//corner Mark
    [[UIApplication sharedapplication] schedulelocalnotification:noti];
when the user clicks on the launch of the notification to enter the app, run the following method:
Receive local push and click to enter app
-(void) Application: (UIApplication *) application didreceivelocalnotification: ( Uilocalnotification *) Notification {

    //here is where the user clicks in via the notification you have launched
    //You can do whatever you like, such as
    //Reset the corner mark, direct to the page generated by the notification 、、、

}
Play system vibration + sound and cyclic vibration + bells

Direct code:

Static PlaySystemSound *_instance; @implementation PlaySystemSound + (PlaySystemSound *) instance{if (!_instance) {_instance = [[Playsystemsoun]
    D Alloc]init];
} return _instance; }//parameter can not pass, play default//If play custom sound, can not exceed 30s length-(ID) Initsystemsoundwithname: (NSString *) soundname soundtype: (NSString *)
    Soundtype {self = [playsystemsound Instance]; if (self) {nsstring *path = [NSString stringwithformat:@ "/system/library/audio/uisounds/%@.%@", soundName?soundNam
        e:@ "Shake", soundtype?soundtype:@ "CAF"]; if (path) {Osstatus error = Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl Fileurlwithpath:path],

            &cafsound);
            if (Error! = Kaudioservicesnoerror) {//Gets the sound of the time, an error occurs cafsound =-1;
}}} return self;

}//The simplest ring, do not need to manage the resources, do not need to stop themselves-(void) shortplay{audioservicesplaysystemsound (cafsound);}
   Call the loop of the system to play, need to stop-(void) Play {//systemaudiocallback, is a C-type function, each time the end of playback is removed with  Audioservicesaddsystemsoundcompletion (cafsound, NULL, NULL, systemaudiocallback, NULL);
    Audioservicesplaysystemsound (ksystemsoundid_vibrate);
Audioservicesplaysystemsound (Cafsound); }//I am here directly delay 1.2s stop playing//that is, no matter how long it plays and where it plays, it will directly remove void Systemaudiocallback () {Audioservicesplaysystemsound (
    Ksystemsoundid_vibrate); Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.2 * nsec_per_sec)), Dispatch_get_main_queue (), ^{[_in
    Stance stop];
});
    }-(void) stop{//Audioservicesremovesystemsoundcompletion (ksystemsoundid_vibrate);
Audioservicesremovesystemsoundcompletion (Cafsound); } * * The following method is called the System Private API, when the shelves AppStore will be a trouble. So, whether it's good or not, I only consider it as a rising posture.
* *//void audioservicesstopsystemsound (int);
void audioservicesplaysystemsoundwithvibration (int, id, nsdictionary *); -(void) playsoundwithpriviteapi{//nsmutabledictionary* dict = [nsmutabledictionary dictionary];//Nsmutablea
rray* arr = [Nsmutablearray array]; ///[arr Addobject:[nsnumber Numberwithbool:yes]];
Vibrate for 2000ms//[arr addobject:[nsnumber numberwithint:2000];
///[Dict setobject:arr forkey:@ "Vibepattern"];
[Dict setobject:[nsnumber numberwithfloat:0.3] forkey:@ "Intensity"];
////Audioservicesstopsystemsound (ksystemsoundid_vibrate);
Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionary];
Nsarray *pattern = @[@YES, @30, @NO, @1];
////dictionary[@ "vibepattern"] = pattern;
dictionary[@ "Intensity"] = @1;
//Audioservicesplaysystemsoundwithvibration (ksystemsoundid_vibrate, nil, dictionary);
 //}

Of course, these are just a rookie of the implementation of the way, welcome to comment, put forward a good way to achieve.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.