IOS-Determine if a user is allowed to push notifications (IOS7/IOS8)

Source: Internet
Author: User

(The user-opened push notification type in iOS8 corresponds to uiusernotificationtype(in the code below uiusernotificationsettings The type of the types attribute), iOS7 corresponds to uiremotenotificationtype)

Here is an example of the Uiusernotificationtype for iOS8, such as when a local notification or push/remote notification push, this constant indicates how the app will alert the user (e.g., a combination of Badge,sound,alert)

So how to get it, in IOS8 is through the types property, [[UIApplication sharedapplication] Currentusernotificationsettings].types

For example, we need to know that this property stores all of the push types you specify (Badge,sound,alert), and in figure one we know the bitmask of the push type: (take four-bit binary as an example)

uiusernotificationtypenone = 0, = = 0000 0

Uiusernotificationtypebadge = 1 << 0, = = 0001 1 shift left 0 bit 2^0 = 1

uiusernotificationtypesound = 1 << 1, = = 0010 1 shift left 1 bit 2^1 = 2

uiusernotificationtypealert = 1 << 2, = = 0100 1 shift left 2 bit 2^2 = 4

(Before the teacher taught C language, said, you can also shift the left as a multiply 2, right to remove 2)

If the user tick push to show badge and cue sound, then types value is 3 (1+2) = = 0001 & 0010 = 0011 = = 2^0 + 2 ^1 = 3

Therefore, if the user does not allow push, the value of types must be 0

1 /**2 * Check if user allow local notification of system setting3  *4 * @return Yes-allowed,otherwise,no.5  */6+(BOOL) isallowednotification {7     //iOS8 Check if user allow notification8     if([Uidevice isSystemVersioniOS8]) {//system is iOS89Uiusernotificationsettings *setting =[[UIApplication sharedapplication] currentusernotificationsettings];Ten         if(Uiusernotificationtypenone! =setting.types) { One             returnYES; A         } -}Else{//iOS7 -Uiremotenotificationtype type =[[UIApplication sharedapplication] enabledremotenotificationtypes]; the         if(Uiremotenotificationtypenone! =type) -             returnYES; -     } -      +     returnNO; -}

The following method I added in the category of uidecive, to determine whether the current system version is greater than iOS8 or less than iOS8

1 /**2 * Check if the system version is IOS83  *4 * @return Yes-is ios8,otherwise,below iOS85  */6+(BOOL) isSystemVersioniOS8 {7     //check Systemverson of device8Uidevice *device =[Uidevice Currentdevice];9     floatSysversion =[Device.systemversion Floatvalue];Ten      One     if(Sysversion >=8.0f) { A         returnYES; -     } -     returnNO; the}

IOS-Determine if a user is allowed to push notifications (IOS7/IOS8)

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.