View Source FCM (Android)
Usage
Set your environment variables.
config :pigeon, :fcm, fcm_default: %{ key: "your_fcm_key_here" }
Create a notification packet.
msg = %{"body" => "your message"} n = Pigeon.FCM.Notification.new("your device registration ID", msg)
Send the packet. Pushes are synchronous and return the notification with updated
:status
and:response
keys. If:status
is success,:response
will contain a keyword list of individual registration ID responses.Pigeon.FCM.push(n)
Sending to Multiple Registration IDs
Pass in a list of registration IDs, as many as you want.
msg = %{"body" => "your message"}
n = Pigeon.FCM.Notification.new(["first ID", "second ID"], msg)
Notification Struct
%Pigeon.FCM.Notification{
collapse_key: nil | String.t(),
dry_run: boolean,
message_id: nil | String.t(),
payload: %{...},
priority: :normal | :high,
registration_id: String.t() | [String.t(), ...],
response: [] | [{atom, String.t()}, ...], | atom,
restricted_package_name: nil | String.t(),
status: atom | nil,
time_to_live: non_neg_integer
}
Notifications with Custom Data
FCM accepts both notification
and data
keys in its JSON payload. Set them like so:
notification = %{"body" => "your message"}
data = %{"key" => "value"}
Pigeon.FCM.Notification.new("registration ID", notification, data)
or
Pigeon.FCM.Notification.new("registration ID")
|> put_notification(%{"body" => "your message"})
|> put_data(%{"key" => "value"})
Handling Push Responses
Pass an optional anonymous function as your second parameter.
data = %{message: "your message"} n = Pigeon.FCM.Notification.new(data, "device registration ID") Pigeon.FCM.push(n, fn(x) -> IO.inspect(x) end) {:ok, %Pigeon.FCM.Notification{...}}
Reponses return the notification with an updated response.
on_response = fn(n) -> case n.status do :success -> bad_regids = FCM.Notification.remove?(n) to_retry = FCM.Notification.retry?(n) # Handle updated regids, remove bad ones, etc :unauthorized -> # Bad FCM key error -> # Some other error end end data = %{message: "your message"} n = Pigeon.FCM.Notification.new("your device token", data) Pigeon.FCM.push(n, on_response: on_response)
Error Responses
Slightly modified from FCM Server Reference
Reason | Description |
---|---|
:missing_registration | Missing Registration Token |
:invalid_registration | Invalid Registration Token |
:not_registered | Unregistered Device |
:invalid_package_name | Invalid Package Name |
:authentication_error | Authentication Error |
:mismatch_sender_id | Mismatched Sender |
:invalid_json | Invalid JSON |
:message_too_big | Message Too Big |
:invalid_data_key | Invalid Data Key |
:invalid_ttl | Invalid Time to Live |
:unavailable | Timeout |
:internal_server_error | Internal Server Error |
:device_message_rate_exceeded | Message Rate Exceeded |
:topics_message_rate_exceeded | Topics Message Rate Exceeded |
:unknown_error | Unknown Error |