iOS technology point: Determine if the user is enabled for push switch/Is it allowed to push?

Source: Internet
Author: User

- ( BOOL ) Isallowednotification {
//ios8 Check if user allow notification
if (is_ios_8) {//System is iOS8
Uiusernotificationsettings *setting = [[UIApplication sharedapplication] currentusernotificationsettings];
if (Uiusernotificationtypenone! = setting.types) {
return YES;
}
}
Else {//ios7
Uiremotenotificationtype type = [[UIApplication sharedapplication] enabledremotenotificationtypes];
if(Uiremotenotificationtypenone! = type)
return YES;
}

return NO;
}

(The type of push notification that the user opens in iOS8 corresponds to uiusernotificationtype (the type of types attribute uiusernotificationsettings in the code below), 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

iOS technology point: Determine if the user is enabled for push switch/Is it allowed to push?

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.