IOS technical point: determines whether the user has enabled the push switch/whether push is allowed ?, Ios Switch

Source: Internet
Author: User

IOS technical point: determines whether the user has enabled the push switch/whether push is allowed ?, Ios Switch
-(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 = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
If(UIRemoteNotificationTypeNone! = Type)
Return YES;
}

Return NO;
}

(The push notification type enabled by the user in iOS8 corresponds to UIUserNotificationType (the type of the types attribute of UIUserNotificationSettings in the code below), and iOS7 corresponds to UIRemoteNotificationType)

This example uses iOS8's UIUserNotificationType. For example, when a local notification or push/remote notification is pushed, this constant specifies how the app notifies users (such as Badge, Sound, alert combination)

So how can we get it? In iOS8, we use the types attribute, [[UIApplication sharedApplication] currentUserNotificationSettings]. types

For example, after obtaining the information, we need to know that this property stores all the push types you specified (Badge, Sound, Alert ), in Figure 1, we know the bitmask corresponding to the push type: (taking the four-bit binary as an example)

UIUserNotificationTypeNone = 0, == 0000 0

UIUserNotificationTypeBadge = 1 <0, = 0001 1 0 shifted left 2 ^ 0 = 1

UIUserNotificationTypeSound = 1 <1, = 0010 1 shifts 1 to the left, 2 ^ 1 = 2

UIUserNotificationTypeAlert = 1 <2, = 0100 1 shifted left 2 ^ 2 = 4

(When the teacher taught the C language, he said that the Left shift can also be used as a multiplication 2, and the right is removed 2)

If the badge and sound prompt are displayed when you select push, the value of types is 3 (1 + 2) = 0001 & 0010 = 0011 = 2 ^ 0 + 2 ^ 1 = 3


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

Related Article

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.