Browse Source

Merge pull request #1287 from barart/fix-anonymous-read-restriction

Handle anonymous read restrictions by sending a poll_request event
Philipp C. Heckel 9 tháng trước cách đây
mục cha
commit
faa4dcbcee
1 tập tin đã thay đổi với 30 bổ sung8 xóa
  1. 30 8
      server/server_firebase.go

+ 30 - 8
server/server_firebase.go

@@ -175,13 +175,26 @@ func toFirebaseMessage(m *message, auther user.Auther) (*messaging.Message, erro
 		} else {
 			// If anonymous read for a topic is not allowed, we cannot send the message along
 			// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
+			//App function needs all the data to create a message object, if not, it fails,
+			//so we set it but put a placeholders to not to send the actual message
+			//but generic title and message instead, we also add the poll_id so client knowns
+			//what message is goint to "decode" (retrieve)
 			data = map[string]string{
-				"id":    m.ID,
-				"time":  fmt.Sprintf("%d", m.Time),
-				"event": pollRequestEvent,
-				"topic": m.Topic,
-			}
-			// TODO Handle APNS?
+                                "id":    m.ID,
+                                "time":  fmt.Sprintf("%d", m.Time),
+                                "event": pollRequestEvent,
+                                "topic": m.Topic,
+                                "priority":     fmt.Sprintf("%d", m.Priority),                                            
+                                "tags":         strings.Join(m.Tags, ","),                                                
+                                "click":        m.Click,                                                                  
+                                "icon":         m.Icon,                                                                      
+                                "title":        "Private",                                                                     
+                                "message":      "Message",                                                                
+                                "content_type": m.ContentType,                                                            
+                                "encoding":     m.Encoding,
+                                "poll_id": m.ID,
+                        }
+                        apnsConfig = createAPNSAlertConfig(m, data)
 		}
 	}
 	var androidConfig *messaging.AndroidConfig
@@ -225,14 +238,23 @@ func createAPNSAlertConfig(m *message, data map[string]string) *messaging.APNSCo
 	for k, v := range data {
 		apnsData[k] = v
 	}
+	alertTitle := m.Title
+        alertBody := maybeTruncateAPNSBodyMessage(m.Message)
+        // If the event is pollRequestEvent (server/topic is restricted) we dont want to
+	//send the actual message to Firebase/APNS, so we send a generic text
+	//if for some reason, client cant retrieve the message, it shows this as the message and title
+        if event, ok := data["event"]; ok && event == pollRequestEvent {
+                alertTitle = "New Notification received"
+                alertBody = "Message cant be retrieved, open the app and refresh content"
+        }
 	return &messaging.APNSConfig{
 		Payload: &messaging.APNSPayload{
 			CustomData: apnsData,
 			Aps: &messaging.Aps{
 				MutableContent: true,
 				Alert: &messaging.ApsAlert{
-					Title: m.Title,
-					Body:  maybeTruncateAPNSBodyMessage(m.Message),
+					Title: alertTitle,
+					Body:  alertBody,
 				},
 			},
 		},