Skip to content

Commit 94b51ab

Browse files
committed
cloud messaging post updated
1 parent 9b2d8a5 commit 94b51ab

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

_drafts/2019-04-22-cloud_messaging.md

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ categories: ["Firebase"]
66
image: firebase/cloud_messaging
77
github: firebase/tree/master/cloud_messaging
88
description: "Firebase"
9-
version: Firebase-Firestore 17.3
10-
keywords: "firebase, chmura, cloud, message, notification, data, android, programowanie, programming"
9+
version: Firebase-Messaging 17.3
10+
keywords: "firebase, chmura, cloud, message, notification, data, token, topic, send, receive, android, programowanie, programming"
1111
---
1212

1313
## Wiadomość
@@ -81,7 +81,7 @@ class FcmTokenService : FirebaseMessagingService() {
8181

8282
{% highlight xml %}
8383
<!-- register service -->
84-
<service android:name=".java.FcmTokenService">
84+
<service android:name=".FcmTokenService">
8585
<intent-filter>
8686
<action android:name="com.google.firebase.MESSAGING_EVENT" />
8787
</intent-filter>
@@ -131,36 +131,69 @@ class FcmTokenService : FirebaseMessagingService() {
131131
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
132132
//check message contains data payload
133133
remoteMessage?.data?.isNotEmpty()?.let {
134-
//prepare some action
134+
//prepare some action like put data into variables
135135
}
136136

137137
//check message contains notification payload
138138
remoteMessage?.notification?.let {
139139
//prepare some action like show notification
140+
showNotification(it)
140141
}
141142
}
142143

143144
override fun onDeletedMessages() {
144145
//do something like server sync for deleted and not received message
145146
}
147+
148+
private fun showNotification(notification: RemoteMessage.Notification) {
149+
//create action on tap
150+
val intent = Intent(this, NotificationActionActivity::class.java).apply {
151+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
152+
}
153+
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, 0)
154+
155+
//build notification
156+
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
157+
.setSmallIcon(R.drawable.ic_launcher_background)
158+
.setContentTitle(notification.title)
159+
.setContentText(notification.body)
160+
.setContentIntent(pendingIntent)
161+
.setAutoCancel(true)
162+
163+
//show notification
164+
NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, builder.build())
165+
}
166+
}
167+
168+
class LauncherActivity : AppCompatActivity() {
169+
170+
override fun onCreate(savedInstanceState: Bundle?) {
171+
super.onCreate(savedInstanceState)
172+
setContentView(R.layout.activity_launcher)
173+
174+
//check are some keys from notification exists
175+
if(intent.extras.containsKey("key1")) {
176+
//do some action depends on keys value like launch another activity
177+
}
178+
}
146179
}
147180
{% endhighlight %}
148181

149182
## Grupowanie
150-
Wiadomości mogą być wysyłane do grupy użytkowników na podstawie informacji przypisanych do konta, zapisanych do tematu (`topic`) lub należących do grupy urządzeń (zawiera dodatkowo autentykacje). W celu zapisania i wypisania klienta do tematu należy wywołać metodę `subscribeToTopic` lub `unsubscribeFromTopic`.
183+
Wiadomości mogą być wysyłane do grupy użytkowników na podstawie informacji przypisanych do konta, zapisanych do tematu (`topic`) lub należących do grupy urządzeń. W celu zapisania i wypisania klienta do tematu należy wywołać metodę `subscribeToTopic` lub `unsubscribeFromTopic`.
151184

152185
{% highlight kotlin %}
153-
fun subscribeToTopic() {
154-
FirebaseMessaging.getInstance().subscribeToTopic("title").addOnCompleteListener { task ->
186+
private fun subscribeToTopic() {
187+
FirebaseMessaging.getInstance().subscribeToTopic("topic").addOnCompleteListener { task ->
155188
if (!task.isSuccessful) {
156189
//subscribe failed
157190
}
158191
//subscribe success
159192
}
160193
}
161194

162-
fun unsubscribeToTopic() {
163-
FirebaseMessaging.getInstance().unsubscribeFromTopic("title").addOnCompleteListener { task ->
195+
private fun unsubscribeFromTopic() {
196+
FirebaseMessaging.getInstance().unsubscribeFromTopic("topic").addOnCompleteListener { task ->
164197
if (!task.isSuccessful) {
165198
//unsubscribe failed
166199
}

0 commit comments

Comments
 (0)