firebase-cloud- messaging - rip tutorial · firebase "" . . examples firebase cloud...

12
firebase-cloud- messaging #firebase- cloud- messaging

Upload: others

Post on 22-Jul-2020

61 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

firebase-cloud-

messaging

#firebase-

cloud-

messaging

Page 2: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

1

1: firebase-cloud-messaging 2

2

Examples 3

3

2: Firebase 4

4

Examples 4

cURL 4

4

3: 8

Examples 8

Android 8

iOS 8

8

10

Page 3: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: firebase-cloud-messaging

It is an unofficial and free firebase-cloud-messaging ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official firebase-cloud-messaging.

The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected]

https://riptutorial.com/ko/home 1

Page 4: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

1: firebase-cloud-messaging " ". : . FCM . 3 :

Firebase 1. Firebase FCM 2. FCM .3.

FCM . notification , data , notification and data . JSON .

{ "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark", "icon" : "myicon" }, "data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } }

notification data . FCM .

notification notification data data1. 3 .2.

notification .

, notification . Android ( NotificationCompat) .

. .

notification , . . onMessageReceived .1. data .2. notification data data . getIntent().getStringExtra("yourKey"); . ( ) onMessageReceived data .

3.

onMessageReceived . .

notification remoteMessage.getNotification() remoteMessage.getNotification() . remoteMessage.getNotification()

1.

data remoteMessage.getData().get("yourKey") .2.

null . . notification data null . data type .

Firebase data .

notification . String .

https://riptutorial.com/ko/home 2

Page 5: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

Firebase "" . .

Examples

Firebase Cloud Messaging Firebase . , Android IOS . .

FCM .

. Android Studio Tools / Firebase Firebase . " " 1 2 .

Firebase , 1 . Firebase . Firebase google-service.json . "app" .1. Google Firebase gradle .2.

FCM . "" FCM ( ).

FCM 2 . . :

FirebaseMessagingService onMessageReceived .1. FirebaseInstanceIdService onTokenRefresh .2. . application </ intent-filter> </ intent-filter>3.

onMessageReceived notification data . onTokenRefresh FCM FCM . FCM ID .

.

firebase-cloud-messaging : https://riptutorial.com/ko/firebase-cloud-messaging/topic/8197/firebase-cloud-messaging-

https://riptutorial.com/ko/home 3

Page 6: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

2: Firebase FCM ( Firebase Cloud Messaging ) . FCM . . 4KB . : https://firebase.google.com/docs/cloud-messaging/

Examples

cURL

cURL FCM REST API .

curl --header "Authorization: key=<API_KEY>" \ --header Content-Type:"application/json" \ https://fcm.googleapis.com/fcm/send \ -d "{\"registration_ids\":[\"ABC\"]}"

.

API_KEY Firebase .

:

"{\"registration_ids\":[\"ABC\"]}"

, . FCM HTTP .

.

POST .1. .

Content-Type = application / json•= < FCM > ( Firebase )•

2.

Body ( raw . (2) ).3. https://fcm.googleapis.com/fcm/send .4.

:

(1)

https://riptutorial.com/ko/home 4

Page 7: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

: . .

(2)

https://riptutorial.com/ko/home 5

Page 8: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

()

https://riptutorial.com/ko/home 6

Page 10: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

3:

Examples

Android

@Override public void onMessageReceived(RemoteMessage remoteMessage) { // ... // TODO(developer): Handle FCM messages here. Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. }

iOS

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // 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. // TODO: Handle data of notification // Print message ID. NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); // Print full message. NSLog(@"%@", userInfo); }

Firebase ( ) () .

onMessageReceived . . [ 1 ]

title body onMessageReceived . Manifest.xml activity .

<intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/>

https://riptutorial.com/ko/home 8

Page 11: firebase-cloud- messaging - RIP Tutorial · Firebase "" . . Examples Firebase Cloud Messaging Firebase . , Android IOS . . FCM .. Android Studio Tools / Firebase Firebase . " " 1

</intent-filter>

intent data .

if (getIntent() != null && getIntent().getExtras() != null) { String customString = (String) getIntent().getExtras().get("myStringData"); Integer customInteger = (Integer) getIntent().getExtras().get("myIntData"); }

Manifest.xml [ 2 ] .

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/your_drawable_icon" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/your_color" />

1 : FCM

2 : FCM github

: https://riptutorial.com/ko/firebase-cloud-messaging/topic/8888/--

https://riptutorial.com/ko/home 9