登录后

时间:2015-05-15 02:44:40

标签: ios push-notification onesignal

我想在他/她登录后向特定用户发送标签,以便他/她可以接收通知。只有登录的用户才会收到通知。 当他/她退出时,我会删除他/她的标签。 我怎么能这样做?

我在AppDelegate中的代码:

let oneSignal: OneSignal = OneSignal(launchOptions: launchOptions, appId: "<my-app-id>") {
        (message, additionalData, isActive) in

        if (additionalData != nil) {
            NSLog("APP LOG ADDITIONALDATA: %@", additionalData);
            let displayMessage: NSString = NSString(format:"NotificationMessage:%@", message);

            var messageTitle: NSString = "";
            if (additionalData["discount"] != nil) {
                messageTitle = additionalData["discount"] as String
            }
            else if (additionalData["bonusCredits"] != nil) {
                messageTitle = additionalData["bonusCredits"] as String;
            }
            else if (additionalData["actionSelected"] != nil) {
                messageTitle = NSString(format:"Pressed ButtonId:%@", additionalData["actionSelected"] as String);
            }

            var alertView: UIAlertView = UIAlertView(title: messageTitle,
                message:displayMessage,
                delegate:self,
                cancelButtonTitle:"Close");

            alertView.show();
        }

        else if (isActive) {
            var alertView: UIAlertView = UIAlertView(title:"OneSignal Message",
                message:message,
                delegate:self,
                cancelButtonTitle:"Close");
            alertView.show();
        }
    }

我的LogInViewController中的代码:

let oneSignal = OneSignal()
oneSignal.sendTag("username", value: self.usernameTextField.text)

我的appDelegate中的代码运行正常,我的用户已收到通知。但即使他们没有登录,他们也可以收到通知。

2 个答案:

答案 0 :(得分:9)

您需要在LogInViewController中使用AppDelegate中的相同oneSignal实例。您可以在类级别创建oneSignal静态,以便可以在两个类之间共享。

要删除标记,您可以调用oneSignal.deleteTag("username")

<强>更新 从iOS 2.0 SDK开始,OneSignal类上的所有方法现在都是静态的。

答案 1 :(得分:1)

Send tag: After login
[oneSignal sendTag:@"key" value:@"value"];

deletetag: After logout
[oneSignal deleteTag:@"key"];


Update 
Onesignal.sendtag();  //for sending that is inserting tag in one signal
Onesignal.deteletag(); //delete that specific tag from one signal db