簡體   English   中英

如何獲取推送通知的iPhone設備令牌?

[英]How to get iphone device token for push notification?

我正在嘗試在我的應用程序中使用遠程推送通知,我試圖在我的應用程序中進行此測試,但在我開始點擊“允許”后我無法重置警報彈出窗口。

所以我的問題是:

即使用戶在警告彈出窗口中點擊“不允許”,我仍然可以獲得設備令牌嗎?

使用appDelegate方法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        self.mDeviceToken = deviceToken;

        //Removing the brackets from the device token
        NSString *tokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

        NSLog(@"Push Notification tokenstring is %@",tokenString);

    }   

以防萬一

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

    NSString* s=[[NSString alloc] initWithFormat:@"%@",error];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:s delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [s release];
// lert because your device will not show log
}

使用以下代理方法...

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

    NSLog(@">>%@",deviceToken);// this will give  you token
}

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

    NSLog(@">>%@",error); // this will gave you error msg with description.

}

希望這個能對您有所幫助..

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

{

    NSLog(@"My token is: %@", deviceToken);

}

通過這種方式,您可以獲得iPhone設備令牌

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM