Firebase向iOS

时间:2017-06-25 19:45:38

标签: ios firebase firebase-cloud-messaging google-cloud-functions firebase-notifications

您好我正在使用firebase来管理通知,但是当我使用index.js处理消息时,sendToTopic函数会发送通知而不检查主题。

所以我试着用firebase的控制台发送消息,它也是一样的。 在用户注册期间,在iOS环境中,我不会订阅任何用户,但仍会收到通知。

我的index.js是:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('topics/{code}/')
.onWrite(event =>{

var request              = event.data.val();
var signals              = request.signals;
var index                = Object.keys(signals).length-1;
var lastSignalKey        = Object.keys(signals)[index];
var lastSignal           = signals[lastSignalKey];
var code                 = lastSignal.code;
var uid                  = lastSignal.uid;
var type                 = lastSignal.type;
var when                 = lastSignal.when;
var sound                = "sound.aiff";


var payload = {
    notification : {
        body : "mMessage",
        title : "MoVeng",
        "sound" : sound
    },
    data: {
        uid:  uid,
        code: code,
        type: type,
        when: when
    }
};


let signal = admin.database()
                    .ref("topics/"+ 
                         code + "/"+
                         type+ "/"+
                         uid);
signal.remove(); 

admin.messaging().sendToTopic(code, payload).then(function(response){
    console.log("Success: ",response);
})
    .catch(function(error){
    console.log("Error: ",error);
})
});

我试过谷歌搜索,但没有人有这个问题。

我不明白为什么如果没有ID,没有处理程序,没有令牌通过通知,只是主题名称,并且sendToTopic没有做它应该做的事情,通知会进来。

这是我的appDelegate代码,如果它可以帮助别人帮助我:

import UIKit
import Firebase
import FirebaseDatabase
import UserNotifications
import FirebaseMessaging

 @UIApplicationMain
 class AppDelegate: UIResponder,    UIApplicationDelegate,UNUserNotificationCenterDelegate {
  public static var active = false
  var window: UIWindow?
  let sharedInstance = FirebaseAPI()
  let defaults = UserDefaults.standard
  var data : NSDictionary?

override init(){
    super.init()

    let logs = defaults.object(forKey: "logs")
    print(String(describing: logs))
    UIFont.overrideInitialize()
}
func getData() -> NSDictionary?{
    return data
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {



    if let options = launchOptions
    {
        let params = options[.remoteNotification] as! NSDictionary
        data = params


    }
    else
    {
        application.applicationIconBadgeNumber = 0
    }
    FirebaseApp.configure()
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (authenticated, error) in
    }
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)

    }
    application.registerForRemoteNotifications()

    return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    sharedInstance.findCode(forUser: (Auth.auth().currentUser?.uid)!) { (code) in
        if let notificationCode = notification.request.content.userInfo["code"] as! String?
        {
            if (notificationCode.isEqual(code))
            {
                completionHandler([.badge, .alert, .sound])
            }
        }
    }

}





func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let request = response.notification.request
    print("mah")
    if response.actionIdentifier == "moveng"
    {
        let whoSignaledMe = request.content.userInfo["whoSignaledMe"] as? String
        let signal = Signal.getAnswer(code: whoSignaledMe!)
        FirebaseAPI.getNotificationKey(forUser: whoSignaledMe!,
                                        ofType:  "answers").setValue(signal.get());
       }
       completionHandler()
   } 


     func application(_ application: UIApplication,   didReceiveRemoteNotification userInfo: [AnyHashable: Any],   fetchCompletionHandler completionHandler: @escaping   (UIBackgroundFetchResult) -> Void) {

    completionHandler(UIBackgroundFetchResult.newData)
   }


}

我哪里错了?

0 个答案:

没有答案