Skip to content

Commit e4048e4

Browse files
authored
Merge pull request #201755 from jiminwen-msft/patch-1
Updating quick start for push notification
2 parents 3278dea + 3e674dd commit e4048e4

File tree

2 files changed

+232
-148
lines changed

2 files changed

+232
-148
lines changed

articles/communication-services/quickstarts/chat/includes/chat-android.md

Lines changed: 1 addition & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -296,154 +296,7 @@ chatAsyncClient.addEventHandler(ChatEventType.CHAT_MESSAGE_RECEIVED, (ChatEvent
296296
> Note with above update, if the application tries to touch any of the notification API like `chatAsyncClient.startRealtimeNotifications()` or `chatAsyncClient.addEventHandler()`, there will be a runtime error.
297297
298298
### Push notifications
299-
300-
> [!NOTE]
301-
> Currently chat push notifications are only supported for Android SDK in version 1.1.0-beta.4.
302-
303-
Push notifications let clients to be notified for incoming messages and other operations occurring in a chat thread in situations where the mobile app is not running in the foreground. Azure Communication Services supports a [list of events that you can subscribe to](../../../concepts/chat/concepts.md#push-notifications).
304-
305-
1. Set up Firebase Cloud Messaging with ChatQuickstart project. Complete steps `Create a Firebase project`, `Register your app with Firebase`, `Add a Firebase configuration file`, `Add Firebase SDKs to your app`, and `Edit your app manifest` in [Firebase Documentation](https://firebase.google.com/docs/cloud-messaging/android/client).
306-
307-
2. Create a Notification Hub within the same subscription as your Communication Services resource, configure your Firebase Cloud Messaging settings for the hub, and link the Notification Hub to your Communication Services resource. See [Notification Hub provisioning](../../../concepts/notifications.md#notification-hub-provisioning).
308-
3. Create a new file `MyFirebaseMessagingService.java` in the same path of file `MainActivity.java`. Copy the following code into file `MyFirebaseMessagingService.java`. You need to replace `<your_package_name>` with the package name used in `MainActivity.java`. You can use your own value for `<your_intent_name>`. This value would be used in step 6 below.
309-
310-
```java
311-
package <your_package_name>;
312-
313-
import android.content.Intent;
314-
import android.util.Log;
315-
316-
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
317-
318-
import com.azure.android.communication.chat.models.ChatPushNotification;
319-
import com.google.firebase.messaging.FirebaseMessagingService;
320-
import com.google.firebase.messaging.RemoteMessage;
321-
322-
import java.util.concurrent.Semaphore;
323-
324-
public class MyFirebaseMessagingService extends FirebaseMessagingService {
325-
private static final String TAG = "MyFirebaseMsgService";
326-
public static Semaphore initCompleted = new Semaphore(1);
327-
328-
@Override
329-
public void onMessageReceived(RemoteMessage remoteMessage) {
330-
try {
331-
Log.d(TAG, "Incoming push notification.");
332-
333-
initCompleted.acquire();
334-
335-
if (remoteMessage.getData().size() > 0) {
336-
ChatPushNotification chatPushNotification =
337-
new ChatPushNotification().setPayload(remoteMessage.getData());
338-
sendPushNotificationToActivity(chatPushNotification);
339-
}
340-
341-
initCompleted.release();
342-
} catch (InterruptedException e) {
343-
Log.e(TAG, "Error receiving push notification.");
344-
}
345-
}
346-
347-
private void sendPushNotificationToActivity(ChatPushNotification chatPushNotification) {
348-
Log.d(TAG, "Passing push notification to Activity: " + chatPushNotification.getPayload());
349-
Intent intent = new Intent("<your_intent_name>");
350-
intent.putExtra("PushNotificationPayload", chatPushNotification);
351-
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
352-
}
353-
}
354-
355-
```
356-
357-
4. At the top of file `MainActivity.java`, add the following import:
358-
359-
```java
360-
import android.content.BroadcastReceiver;
361-
import android.content.Context;
362-
import android.content.Intent;
363-
import android.content.IntentFilter;
364-
365-
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
366-
import com.azure.android.communication.chat.models.ChatPushNotification;
367-
import com.google.android.gms.tasks.OnCompleteListener;
368-
import com.google.android.gms.tasks.Task;
369-
import com.google.firebase.messaging.FirebaseMessaging;
370-
```
371-
372-
5. Add the following code into class `MainActivity`:
373-
374-
```java
375-
private BroadcastReceiver firebaseMessagingReceiver = new BroadcastReceiver() {
376-
@Override
377-
public void onReceive(Context context, Intent intent) {
378-
ChatPushNotification pushNotification =
379-
(ChatPushNotification) intent.getParcelableExtra("PushNotificationPayload");
380-
381-
Log.d(TAG, "Push Notification received in MainActivity: " + pushNotification.getPayload());
382-
383-
boolean isHandled = chatAsyncClient.handlePushNotification(pushNotification);
384-
if (!isHandled) {
385-
Log.d(TAG, "No listener registered for incoming push notification!");
386-
}
387-
}
388-
};
389-
390-
391-
private void startFcmPushNotification() {
392-
FirebaseMessaging.getInstance().getToken()
393-
.addOnCompleteListener(new OnCompleteListener<String>() {
394-
@Override
395-
public void onComplete(@NonNull Task<String> task) {
396-
if (!task.isSuccessful()) {
397-
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
398-
return;
399-
}
400-
401-
// Get new FCM registration token
402-
String token = task.getResult();
403-
404-
// Log and toast
405-
Log.d(TAG, "Fcm push token generated:" + token);
406-
Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
407-
408-
chatAsyncClient.startPushNotifications(token, new Consumer<Throwable>() {
409-
@Override
410-
public void accept(Throwable throwable) {
411-
Log.w(TAG, "Registration failed for push notifications!", throwable);
412-
}
413-
});
414-
}
415-
});
416-
}
417-
418-
```
419-
420-
6. Update function `onCreate` in class `MainActivity`.
421-
422-
```java
423-
@Override
424-
protected void onCreate(Bundle savedInstanceState) {
425-
super.onCreate(savedInstanceState);
426-
setContentView(R.layout.activity_main);
427-
428-
LocalBroadcastManager
429-
.getInstance(this)
430-
.registerReceiver(
431-
firebaseMessagingReceiver,
432-
new IntentFilter("<your_intent_name>"));
433-
}
434-
```
435-
436-
7. Put the following code below comment `<RECEIVE CHAT MESSAGES>`:
437-
438-
```java
439-
startFcmPushNotification();
440-
441-
chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_RECEIVED, (ChatEvent payload) -> {
442-
Log.i(TAG, "Push Notification CHAT_MESSAGE_RECEIVED.");
443-
ChatMessageReceivedEvent event = (ChatMessageReceivedEvent) payload;
444-
// You code to handle ChatMessageReceived event
445-
});
446-
```
299+
Please check out [Android push notifications](../../../tutorials/chat-android-push-notification.md) for details.
447300
448301
## Add a user as a participant to the chat thread
449302
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
---
2+
title: Enable push notifications in your Android chat app
3+
titleSuffix: An Azure Communication Services tutorial
4+
description: Learn how to enable push notification in Android App by using Azure Communication Chat SDK
5+
author: jiminwen
6+
services: azure-communication-services
7+
ms.author: jiminwen
8+
ms.date: 08/16/2022
9+
ms.topic: tutorial
10+
ms.service: azure-communication-services
11+
---
12+
13+
14+
# Enable push notifications
15+
Push notifications let clients be notified for incoming messages and other operations occurring in a chat thread in situations where the mobile app isn't running in the foreground. Azure Communication Services supports a [list of events that you can subscribe to](../concepts/chat/concepts.md#push-notifications).
16+
> [!NOTE]
17+
> Chat push notifications are supported for Android SDK in versions starting from 1.1.0-beta.4 and 1.1.0. It is recommended that you use version 1.2.0 or newer, as older versions have a known issue with the registration renewal. Steps from 8 to 12 are only needed for versions equal to or greater than 1.2.0.
18+
19+
1. Set up Firebase Cloud Messaging for the ChatQuickstart project. Complete steps `Create a Firebase project`, `Register your app with Firebase`, `Add a Firebase configuration file`, `Add Firebase SDKs to your app`, and `Edit your app manifest` in [Firebase Documentation](https://firebase.google.com/docs/cloud-messaging/android/client).
20+
21+
2. Create a Notification Hub within the same subscription as your Communication Services resource, configure your Firebase Cloud Messaging settings for the hub, and link the Notification Hub to your Communication Services resource. See [Notification Hub provisioning](../concepts/notifications.md#notification-hub-provisioning).
22+
3. Create a new file called `MyFirebaseMessagingService.java` in the same directory where `MainActivity.java` resides. Copy the following code into `MyFirebaseMessagingService.java`. You will need to replace `<your_package_name>` with the package name used in `MainActivity.java`. You can use your own value for `<your_intent_name>`. This value will be used in step 6 below.
23+
24+
```java
25+
package <your_package_name>;
26+
27+
import android.content.Intent;
28+
import android.util.Log;
29+
30+
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
31+
32+
import com.azure.android.communication.chat.models.ChatPushNotification;
33+
import com.google.firebase.messaging.FirebaseMessagingService;
34+
import com.google.firebase.messaging.RemoteMessage;
35+
36+
import java.util.concurrent.Semaphore;
37+
38+
public class MyFirebaseMessagingService extends FirebaseMessagingService {
39+
private static final String TAG = "MyFirebaseMsgService";
40+
public static Semaphore initCompleted = new Semaphore(1);
41+
42+
@Override
43+
public void onMessageReceived(RemoteMessage remoteMessage) {
44+
try {
45+
Log.d(TAG, "Incoming push notification.");
46+
47+
initCompleted.acquire();
48+
49+
if (remoteMessage.getData().size() > 0) {
50+
ChatPushNotification chatPushNotification =
51+
new ChatPushNotification().setPayload(remoteMessage.getData());
52+
sendPushNotificationToActivity(chatPushNotification);
53+
}
54+
55+
initCompleted.release();
56+
} catch (InterruptedException e) {
57+
Log.e(TAG, "Error receiving push notification.");
58+
}
59+
}
60+
61+
private void sendPushNotificationToActivity(ChatPushNotification chatPushNotification) {
62+
Log.d(TAG, "Passing push notification to Activity: " + chatPushNotification.getPayload());
63+
Intent intent = new Intent("<your_intent_name>");
64+
intent.putExtra("PushNotificationPayload", chatPushNotification);
65+
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
66+
}
67+
}
68+
69+
```
70+
71+
4. At the top of file `MainActivity.java`, add the following import statements:
72+
73+
```java
74+
import android.content.BroadcastReceiver;
75+
import android.content.Context;
76+
import android.content.Intent;
77+
import android.content.IntentFilter;
78+
79+
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
80+
import com.azure.android.communication.chat.models.ChatPushNotification;
81+
import com.google.android.gms.tasks.OnCompleteListener;
82+
import com.google.android.gms.tasks.Task;
83+
import com.google.firebase.messaging.FirebaseMessaging;
84+
```
85+
86+
5. Add the following code to the `MainActivity` class:
87+
88+
```java
89+
private BroadcastReceiver firebaseMessagingReceiver = new BroadcastReceiver() {
90+
@Override
91+
public void onReceive(Context context, Intent intent) {
92+
ChatPushNotification pushNotification =
93+
(ChatPushNotification) intent.getParcelableExtra("PushNotificationPayload");
94+
95+
Log.d(TAG, "Push Notification received in MainActivity: " + pushNotification.getPayload());
96+
97+
boolean isHandled = chatAsyncClient.handlePushNotification(pushNotification);
98+
if (!isHandled) {
99+
Log.d(TAG, "No listener registered for incoming push notification!");
100+
}
101+
}
102+
};
103+
104+
105+
private void startFcmPushNotification() {
106+
FirebaseMessaging.getInstance().getToken()
107+
.addOnCompleteListener(new OnCompleteListener<String>() {
108+
@Override
109+
public void onComplete(@NonNull Task<String> task) {
110+
if (!task.isSuccessful()) {
111+
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
112+
return;
113+
}
114+
115+
// Get new FCM registration token
116+
String token = task.getResult();
117+
118+
// Log and toast
119+
Log.d(TAG, "Fcm push token generated:" + token);
120+
Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
121+
122+
chatAsyncClient.startPushNotifications(token, new Consumer<Throwable>() {
123+
@Override
124+
public void accept(Throwable throwable) {
125+
Log.w(TAG, "Registration failed for push notifications!", throwable);
126+
}
127+
});
128+
}
129+
});
130+
}
131+
132+
```
133+
134+
6. Update the function `onCreate` in `MainActivity`.
135+
136+
```java
137+
@Override
138+
protected void onCreate(Bundle savedInstanceState) {
139+
super.onCreate(savedInstanceState);
140+
setContentView(R.layout.activity_main);
141+
142+
LocalBroadcastManager
143+
.getInstance(this)
144+
.registerReceiver(
145+
firebaseMessagingReceiver,
146+
new IntentFilter("<your_intent_name>"));
147+
}
148+
```
149+
150+
7. Put the following code below the comment `<RECEIVE CHAT MESSAGES>` in `MainActivity`:
151+
152+
```java
153+
startFcmPushNotification();
154+
155+
chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_RECEIVED, (ChatEvent payload) -> {
156+
Log.i(TAG, "Push Notification CHAT_MESSAGE_RECEIVED.");
157+
ChatMessageReceivedEvent event = (ChatMessageReceivedEvent) payload;
158+
// You code to handle ChatMessageReceived event
159+
});
160+
```
161+
162+
8. Add the `xmlns:tools` field to the `AndroidManifest.xml` file:
163+
164+
```
165+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
166+
xmlns:tools="http://schemas.android.com/tools"
167+
package="com.azure.android.communication.chat.sampleapp">
168+
```
169+
170+
9. Disable the default initializer for `WorkManager` in `AndroidManifest.xml`:
171+
172+
```
173+
<!-- Disable the default initializer of WorkManager so that we could override it in MyAppConfiguration -->
174+
<provider
175+
android:name="androidx.startup.InitializationProvider"
176+
android:authorities="${applicationId}.androidx-startup"
177+
android:exported="false"
178+
tools:node="merge">
179+
<!-- If you are using androidx.startup to initialize other components -->
180+
<meta-data
181+
android:name="androidx.work.WorkManagerInitializer"
182+
android:value="androidx.startup"
183+
tools:node="remove" />
184+
</provider>
185+
<!-- End of Disabling default initializer of WorkManager -->
186+
```
187+
188+
10. Add the `WorkManager` dependency to your `build.gradle` file:
189+
190+
```
191+
def work_version = "2.7.1"
192+
implementation "androidx.work:work-runtime:$work_version"
193+
```
194+
195+
11. Add a custom `WorkManager` initializer by creating a class implementing `Configuration.Provider`:
196+
197+
```java
198+
public class MyAppConfiguration extends Application implements Configuration.Provider {
199+
Consumer<Throwable> exceptionHandler = new Consumer<Throwable>() {
200+
@Override
201+
public void accept(Throwable throwable) {
202+
Log.i("YOUR_TAG", "Registration failed for push notifications!" + throwable.getMessage());
203+
}
204+
};
205+
@Override
206+
public void onCreate() {
207+
super.onCreate();
208+
WorkManager.initialize(getApplicationContext(), getWorkManagerConfiguration());
209+
}
210+
@NonNull
211+
@Override
212+
public Configuration getWorkManagerConfiguration() {
213+
return new Configuration.Builder().
214+
setWorkerFactory(new RegistrationRenewalWorkerFactory(COMMUNICATION_TOKEN_CREDENTIAL, exceptionHandler)).build();
215+
}
216+
}
217+
```
218+
219+
12. Add the `android:name=.MyAppConfiguration` field, which uses the class name from step 11, into `AndroidManifest.xml`:
220+
221+
```
222+
<application
223+
android:allowBackup="true"
224+
android:icon="@mipmap/ic_launcher"
225+
android:label="@string/app_name"
226+
android:roundIcon="@mipmap/ic_launcher_round"
227+
android:theme="@style/Theme.AppCompat"
228+
android:supportsRtl="true"
229+
android:name=".MyAppConfiguration"
230+
>
231+
```

0 commit comments

Comments
 (0)