简体   繁体   English

为推送通知注册 iPhone 应用程序不起作用

[英]Registering the iPhone application for push notification not working

I have tried out the example found in the link http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 , for my iPhone push notification message.我已经尝试了链接http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 中的示例,用于我的 iPhone 推送通知消息。 I could successfully run all steps except the steps for registering the app for push notification for the first time.除了第一次为推送通知注册应用程序的步骤之外,我可以成功运行所有步骤。

I have followed different variations of the code:我遵循了代码的不同变体:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

But none worked for me.但没有一个对我有用。 I am testing it in my iPhone 4 and my Xode version is 4.2 , OS is Lion.我正在我的 iPhone 4 上测试它,我的 Xode 版本是 4.2 ,操作系统是 Lion。 Please advice.请指教。

You can try the following:您可以尝试以下操作:

  • Make sure your Xcode project is configured to use the explicit bundle ID from the provisioning portal确保您的 Xcode 项目配置为使用配置门户中的显式包 ID
  • Make sure you're not building the target with wildcard or team provisioning profile, check this on the project AND target settings确保您没有使用通配符或团队配置文件构建目标,请检查项目目标设置
  • If you created your provisioning profile before configuring the app ID for push, regenerate the provisioning profile如果您在为推送配置应用 ID 之前创建了配置文件,请重新生成配置文件

Also look at the error object in the UIApplicationDelegate method还要看UIApplicationDelegate方法中的error对象

application:didFailToRegisterForRemoteNotificationsWithError:

EDIT 1编辑 1

Open your provisioning profile in a text editor and look for the string在文本编辑器中打开您的配置文件并查找字符串

<key>aps-environment</key>

If your profile does not contain this, it is not correctly set up for push.如果您的个人资料不包含此内容,则说明未正确设置推送。

Here is my workin code sample from application delegate ( AppDelegate.m )这是我来自应用程序委托 ( AppDelegate.m ) 的工作代码示例

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}

To check if registration went OK, implement these three methods要检查注册是否成功,请实现这三个方法

– application:didReceiveRemoteNotification:
– application:didRegisterForRemoteNotificationsWithDeviceToken:
– application:didFailToRegisterForRemoteNotificationsWithError:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Or you can go to Settings to see if there is permissions settings available或者你可以进入设置查看是否有可用的权限设置

like this:像这样:

iPad 通知设置

If this problem appears in production (with your application on the appStore), it's very likely because you created/enabled the APNS production mode of your application AFTER having created the distribution profile.如果此问题出现在生产中(您的应用程序在 appStore 上),很可能是因为您在创建分发配置文件创建/启用了应用程序的 APNS 生产模式。

In this case, no other option but to submit again with a correct certificate...在这种情况下,别无选择,只能用正确的证书再次提交......

Rule of thumb: ALWAYS enable/create production certificate for your application BEFORE creating the distribution profile.经验法则:创建分发配置文件之前,始终为您的应用程序启用/创建生产证书。

You can use the method described by 'nduplessis' (see below) to manually check if you application will run notification once approved.您可以使用“nduplessis”(见下文)描述的方法来手动检查您的应用程序在获得批准后是否会运行通知。

Go to Capabilities -> Push Notifications .转到功能->推送通知 If there are errors then try out this solution!如果有错误,请尝试此解决方案!

If Xcode version was changed from 7 to 8 then try out this solution!如果Xcode 版本从 7更改为 8,请尝试此解决方案!

Xcode 7 copies the aps-environment entitlement from your provisioning profile. Xcode 7 从您的配置文件中复制 aps-environment 权利。 Turning on push notifications on Xcode 8 adds the entitlement both to your provisioning profile and the entitlements file.在 Xcode 8 上打开推送通知会将权利添加到您的配置文件和权利文件。

Try adding the aps-environment entitlement to your app's entitlements file, or switch the notification capability off and on again.尝试将 aps-environment 权利添加到您的应用程序的权利文件中,或者关闭并重新打开通知功能。

iOS 14 ? iOS 14?

For those who faced with inability to get token, and double checked all the things mentioned above, and developed on iOS 14, and if you have non-english app title, go to Project - Target - Product Name and replace name by $(TARGET_NAME).对于那些面临无法获得令牌的人,并仔细检查上述所有内容,并在 iOS 14 上开发,如果您有非英文应用程序标题,请转到项目 - 目标 - 产品名称并将名称替换为 $(TARGET_NAME )。

I spent entire week on this bug.我在这个错误上花了整整一周的时间。

Once again: if your app receives token on iOS12, but can not on iOS14, get rid of non-latin chars in names.再说一遍:如果您的应用在 iOS12 上收到令牌,但在 iOS14 上不能收到,请去掉名称中的非拉丁字符。 They say, including entitlements, but as for me, I've managed to make it work and with cyrillic entitlements.他们说,包括权利,但就我而言,我已经设法使其工作并使用西里尔文权利。 To change Product name was enough.更改产品名称就足够了。

Have you ever seen the push notification dialog in your app?你有没有在你的应用程序中看到推送通知对话框? If you dismiss the dialog it takes 24 hours to reshow.如果您关闭对话框,则需要 24 小时才能重新显示。 Check this by setting the time on your iPhone ahead by 2 days and see if it shows.通过将 iPhone 上的时间提前 2 天进行检查,看看它是否显示。

Also have you implemented the delegate methods:你还实现了委托方法:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

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

Try putting an NSLog(@"") in the didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError and see it is is getting in there?尝试将 NSLog(@"") 放在 didRegisterForRemoteNotificationsWithDeviceToken 和 didFailToRegisterForRemoteNotificationsWithError 中,看看它是否在那里?

Also you require an App Id that is setup for Push notification and you need to have generated the Push notification certificates to send the messages.您还需要一个为推送通知设置的应用程序 ID,并且您需要生成推送通知证书才能发送消息。

您只能使用具有启用推送通知服务的应用程序 ID 的配置文件注册它。请同时检查这些步骤。这是一个启动推送通知的好教程http://mobiforge.com/developing/story/programming-apple-push -通知服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM