2017-05-23 2 views
1

이 도움에 대해 매우 감사드립니다. "글로벌"주제에 가입 한 모든 사용자에게 내 PHP 코드를 사용하여 모든 사용자에게 알림을 보내고 싶습니다. 아무도 왜 작동하지 않을지 알고 있습니까? 앱을 사용하는 모든 사람들이 알림을 받기를 원하기 때문에 더 좋은 방법이없는 한 모든 사용자를 구독하게됩니다. 나는 지식이 부족하고Firebase 알림을 주제로 보내는 요청이 작동하지 않습니다 (HTTP POST)?

<?php 
    define('API_ACCESS_KEY', 'hidden...hidden'); 

    $msg = array 
    (
     'message' => 'here is a message. message', 
     'title'  => 'This is a title. title', 
     'vibrate' => 1, 
     'sound'  => 1 
    ); 

    $fields = array 
    (
     'to'   => "/topics/global", 
     'data'   => $msg, 
     'priority'  => 'high' 
    ); 

    $headers = array 
    (
     'Authorization: key=' . API_ACCESS_KEY, 
     'Content-Type: application/json' 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
    curl_setopt($ch,CURLOPT_POST, true); 
    curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 

    $result = curl_exec($ch); 
    curl_close($ch); 
    echo $result; 
?> 

을하지만, $ 결과에서 모든 실패 메시지처럼 보이지 않았다 에코 : 여기에 지구 내 주제에 통지를 보내려고 내 PHP입니다. 이것은 내가 가진 것입니다 : 나는 주제에 보내는 내 장치에서 작동하는지 테스트 할 수 있도록 내 중포 기지 콘솔에서

{"message_id":7591682951632927615} 

, 나는 심지어 "글로벌"항목을 볼 수 없습니다. 내가 온라인에서 읽은 것에서는 가입 한 주제가 콘솔에 표시되는 데 잠시 시간이 걸릴 수 있습니다. 앱에 설정된 사용자 세그먼트를 사용하여 모든 장치에 알림을 보내려면 콘솔에서 작동해야합니다.

내 앱이 실제로 사용자를 "글로벌"항목에 가입시키고 있는지 확인할 수있는 방법이 있습니까? 어쩌면 이것이 문제 일 수 있습니다.

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

    FIRApp.configure() 

    if #available(iOS 10.0, *) { 
     let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
     UNUserNotificationCenter.current().requestAuthorization(
      options: authOptions, 
      completionHandler: {_, _ in }) 

     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.current().delegate = self 
     // For iOS 10 data message (sent via FCM) 
     FIRMessaging.messaging().remoteMessageDelegate = self 

    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 

    application.registerForRemoteNotifications() 

    return true 
} 


func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
    print("applicationReceivedRemoteMessage") 
} 


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    if let refreshedToken = FIRInstanceID.instanceID().token() { 
     print("InstanceID token: \(refreshedToken)") 
     FIRMessaging.messaging().subscribe(toTopic: "/topics/global") 
    } 
} 


func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
    // If you are receiving a notification message while your app is in the background, 
    // this callback will not be fired till the user taps on the notification launching the application. 

    /* 
    // Print message ID. 
    if let messageID = userInfo["gcmMessageIDKey"] { 
    print("Message ID: \(messageID)") 
    } 

    // Print full message. 
    print(userInfo) 
    */ 
} 


func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    // If you are receiving a notification message while your app is in the background, 
    // this callback will not be fired till the user taps on the notification launching the application. 

    if application.applicationState == UIApplicationState.active { 

     print("GOT IN HERE") 

     var pushNotificationMessage = "" 
     if let aps = userInfo["aps"] as? NSDictionary { 
      if let alert = aps["alert"] as? NSDictionary { 
       if let message = alert["message"] as? NSString { 
        pushNotificationMessage = message as String 
       } 
      } else if let alert = aps["alert"] as? NSString { 
       pushNotificationMessage = alert as String 
      } 
     } 

     let notificationAlert = UIAlertController(title: nil, message: pushNotificationMessage, preferredStyle: .alert) 

     let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { 
      (alert: UIAlertAction!) -> Void in 
     }) 
     defaultAction.setValue(Constants.activePushNotificationOKColor, forKey: "titleTextColor") 

     notificationAlert.addAction(defaultAction) 
     self.window?.rootViewController?.present(notificationAlert, animated: true, completion: nil) 
    } 
} 
+0

[이 문서는] (https://firebase.google.com/docs/cloud-messaging/http-server-ref)는 약간 다른 URL을 사용합니다. –

답변

1

가하는 notification을 보내 notification에없는 데이터를 매개 변수를 저장하려면 :

$fields = array 
(
    'to'   => "/topics/global", 
    'notification' => $msg, // <= CHANGED 
    'priority'  => 'high' 
); 

은 또한 Notification payload support에 대한 설명서에 표 2A보고 여기에 관련 SWIFT 코드입니다. message은 지원되지 않으므로 대신 body을 사용하십시오. `대신 gcm``의 fcm` :

$msg = array 
(
    'body'  => 'here is a message. message', // <= CHANGED 
    'title'  => 'This is a title. title', 
    'vibrate' => 1, 
    'sound'  => 1 
); 
+0

고맙습니다. gcm으로 남겨 두었습니다 (fcm이 어떤 이유로 404 오류를 주었기 때문에). 그러나 알림으로 변경 한 후 어느 정도 작동합니다. 나는 그 화제가 작동하고 있는지를 확인할 수 있었지만 이상하게도 나는 단지 알림 소리 만들을뿐입니다. –

+0

내 대답의 업데이트를 확인하십시오. –

+0

내 문제가 해결되었습니다. Bob 대단히 감사합니다. –

관련 문제