Skip to content

Commit 2d732e5

Browse files
authored
Merge pull request #84065 from spelluru/nhubxamarinandroid0801
No need of FirebaseInstanceID class
2 parents 873a218 + e47714c commit 2d732e5

File tree

5 files changed

+74
-102
lines changed

5 files changed

+74
-102
lines changed
-72.9 KB
Loading

articles/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm.md

Lines changed: 64 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ms.tgt_pltfrm: mobile-xamarin-android
1414
ms.devlang: dotnet
1515
ms.topic: tutorial
1616
ms.custom: mvc
17-
ms.date: 05/01/2019
17+
ms.date: 08/01/2019
1818
ms.author: jowargo
1919
---
2020

@@ -48,10 +48,10 @@ In this tutorial, you take the following steps:
4848

4949
[!INCLUDE [notification-hubs-portal-create-new-hub](../../includes/notification-hubs-portal-create-new-hub.md)]
5050

51-
### Configure GCM settings for the notification hub
51+
### Configure GCM/FCM settings for the notification hub
5252

53-
1. Select **Google (GCM)** in the **NOTIFICATION SETTINGS** section.
54-
2. Enter the **Legacy server key** you noted down from the Google Firebase Console.
53+
1. Select **Google (GCM/FCM)/** in the **Settings** section on the left menu.
54+
2. Enter the **server key** you noted down from the Google Firebase Console.
5555
3. Select **Save** on the toolbar.
5656

5757
![](./media/notification-hubs-android-get-started/notification-hubs-gcm-api.png)
@@ -214,123 +214,100 @@ Your notification hub is configured to work with FCM, and you have the connectio
214214
CreateNotificationChannel();
215215
```
216216

217-
11. Create a new class, `MyFirebaseIIDService` like you created the `Constants` class.
218-
12. Add the following using statements to `MyFirebaseIIDService.cs`:
219-
220-
```csharp
221-
using Android.Util;
222-
using WindowsAzure.Messaging;
223-
using Firebase.Iid;
224-
```
225-
226-
13. In `MyFirebaseIIDService.cs`, add the following `class` declaration, and have your class inherit from `FirebaseInstanceIdService`:
227-
228-
```csharp
229-
[Service]
230-
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
231-
public class MyFirebaseIIDService : FirebaseInstanceIdService
232-
```
233-
234-
14. In `MyFirebaseIIDService.cs`, add the following code:
235-
236-
```csharp
237-
const string TAG = "MyFirebaseIIDService";
238-
NotificationHub hub;
239-
240-
public override void OnTokenRefresh()
241-
{
242-
var refreshedToken = FirebaseInstanceId.Instance.Token;
243-
Log.Debug(TAG, "FCM token: " + refreshedToken);
244-
SendRegistrationToServer(refreshedToken);
245-
}
246-
247-
void SendRegistrationToServer(string token)
248-
{
249-
// Register with Notification Hubs
250-
hub = new NotificationHub(Constants.NotificationHubName,
251-
Constants.ListenConnectionString, this);
252-
253-
var tags = new List<string>() { };
254-
var regID = hub.Register(token, tags.ToArray()).RegistrationId;
255-
256-
Log.Debug(TAG, $"Successful registration of ID {regID}");
257-
}
258-
```
259-
260-
15. Create another new class for your project, name it `MyFirebaseMessagingService`.
217+
15. Add a class named `MyFirebaseMessagingService` to your project.
261218
16. Add the following using statements to `MyFirebaseMessagingService.cs`.
262219

263220
```csharp
264221
using Android.Util;
265222
using Firebase.Messaging;
266-
using Android.Support.V4.App;
267-
using Build = Android.OS.Build;
223+
using Android.Support.V4.App;
224+
using WindowsAzure.Messaging;
268225
```
269226

270227
17. Add the following above your class declaration, and have your class inherit from `FirebaseMessagingService`:
271228

272229
```csharp
273230
[Service]
274231
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
232+
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
275233
public class MyFirebaseMessagingService : FirebaseMessagingService
276234
```
277235

278-
18. Add the following code to `MyFirebaseMessagingService.cs`:
236+
18. Add the following code to `MyFirebaseMessagingService.cs` to process messages that are received.
279237

280238
```csharp
281-
const string TAG = "MyFirebaseMsgService";
282-
public override void OnMessageReceived(RemoteMessage message)
283-
{
284-
Log.Debug(TAG, "From: " + message.From);
285-
if(message.GetNotification()!= null)
239+
const string TAG = "MyFirebaseMsgService";
240+
NotificationHub hub;
241+
242+
public override void OnMessageReceived(RemoteMessage message)
286243
{
287-
//These is how most messages will be received
288-
Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
289-
SendNotification(message.GetNotification().Body);
244+
Log.Debug(TAG, "From: " + message.From);
245+
if (message.GetNotification() != null)
246+
{
247+
//These is how most messages will be received
248+
Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
249+
SendNotification(message.GetNotification().Body);
250+
}
251+
else
252+
{
253+
//Only used for debugging payloads sent from the Azure portal
254+
SendNotification(message.Data.Values.First());
255+
256+
}
290257
}
291-
else
258+
259+
void SendNotification(string messageBody)
292260
{
293-
//Only used for debugging payloads sent from the Azure portal
294-
SendNotification(message.Data.Values.First());
295-
261+
var intent = new Intent(this, typeof(MainActivity));
262+
intent.AddFlags(ActivityFlags.ClearTop);
263+
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
264+
265+
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID);
266+
267+
notificationBuilder.SetContentTitle("FCM Message")
268+
.SetSmallIcon(Resource.Drawable.ic_launcher)
269+
.SetContentText(messageBody)
270+
.SetAutoCancel(true)
271+
.SetShowWhen(false)
272+
.SetContentIntent(pendingIntent);
273+
274+
var notificationManager = NotificationManager.FromContext(this);
275+
276+
notificationManager.Notify(0, notificationBuilder.Build());
296277
}
297-
}
278+
```
298279

299-
void SendNotification(string messageBody)
300-
{
301-
var intent = new Intent(this, typeof(MainActivity));
302-
intent.AddFlags(ActivityFlags.ClearTop);
303-
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
304-
305-
var notificationBuilder = new NotificationCompat.Builder(this)
306-
.SetContentTitle("FCM Message")
307-
.SetSmallIcon(Resource.Drawable.ic_launcher)
308-
.SetContentText(messageBody)
309-
.SetAutoCancel(true)
310-
.SetShowWhen(false)
311-
.SetContentIntent(pendingIntent);
312-
313-
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
280+
19. Add the following methods to the MyFirebaseMessagingService class to receive the FCM registration token and send it to the Notification Hubs instance (hub).
281+
282+
```csharp
283+
public override void OnNewToken(string token)
314284
{
315-
notificationBuilder.SetChannelId(MainActivity.CHANNEL_ID);
285+
Log.Debug(TAG, "FCM token: " + token);
286+
SendRegistrationToServer(token);
316287
}
317288

318-
var notificationManager = NotificationManager.FromContext(this);
289+
void SendRegistrationToServer(string token)
290+
{
291+
// Register with Notification Hubs
292+
hub = new NotificationHub(Constants.NotificationHubName,
293+
Constants.ListenConnectionString, this);
319294

320-
notificationManager.Notify(0, notificationBuilder.Build());
321-
}
322-
```
295+
var tags = new List<string>() { };
296+
var regID = hub.Register(token, tags.ToArray()).RegistrationId;
323297

324-
19. **Build** your project.
325-
20. **Run** your app on your device or loaded emulator
298+
Log.Debug(TAG, $"Successful registration of ID {regID}");
299+
}
300+
```
301+
1. **Build** your project.
302+
1. **Run** your app on your device or loaded emulator
326303

327304
## Send test notification from the Azure portal
328305

329306
You can test receiving notifications in your app with the **Test Send** option in the [Azure portal]. It sends a test push notification to your device.
330307

331308
![Azure portal - Test Send](media/partner-xamarin-notification-hubs-android-get-started/send-test-notification.png)
332309

333-
Push notifications are normally sent in a back-end service like Mobile Services or ASP.NET through a compatible library. If a library is not available for your back-end, you can also use the REST API directly to send notification messages.
310+
Push notifications are normally sent in a back-end service like Mobile Services or ASP.NET through a compatible library. If a library isn't available for your back-end, you can also use the REST API directly to send notification messages.
334311

335312
## Next steps
336313

101 KB
Loading
46.5 KB
Loading

includes/notification-hubs-enable-firebase-cloud-messaging-xamarin.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
author: spelluru
66
ms.service: notification-hubs
77
ms.topic: include
8-
ms.date: 03/11/2019
8+
ms.date: 08/01/2019
99
ms.author: spelluru
1010
ms.custom: include file
1111
---
@@ -16,28 +16,23 @@
1616
![Add Firebase to your Android app](./media/notification-hubs-enable-firebase-cloud-messaging/notification-hubs-add-firebase-to-android-app.png)
1717

1818
3. On the **Add Firebase to your Android app** page, take the following steps:
19-
1.
2019
1. For the **Android package name**, enter a name for your package. For example: `tutorials.tutoria1.xamarinfcmapp`.
2120

2221
![Specify the package name](./media/notification-hubs-enable-firebase-cloud-messaging/specify-package-name-fcm-settings.png)
22+
2. Select **Register app**.
23+
1. Select **Download google-services.json**. Then save the file into the folder of your project and select **Next**. If you haven't created the Visual Studio project yet, you can do this step after you create the project.
2324

24-
2. Select **Register app**.
25-
1.
26-
1. Select **Download google-services.json**. Then save the file into the **app** folder of your project and select **Next**.
27-
28-
![Download google-services.json](./media/notification-hubs-enable-firebase-cloud-messaging/download-google-service-button.png)
29-
30-
6. Select **Next**.
31-
7. Select **Skip this step**.
32-
33-
![Skip the last step](./media/notification-hubs-enable-firebase-cloud-messaging/skip-this-step.png)
25+
![Download google-services.json](./media/notification-hubs-enable-firebase-cloud-messaging/download-google-service-button.png)
26+
6. Select **Next**.
27+
7. Select **Skip this step**.
3428

29+
![Skip the last step](./media/notification-hubs-enable-firebase-cloud-messaging/skip-this-step.png)
3530
8. In the Firebase console, select the cog for your project. Then select **Project Settings**.
3631

3732
![Select Project Settings](./media/notification-hubs-enable-firebase-cloud-messaging/notification-hubs-firebase-console-project-settings.png)
38-
3933
4. If you haven't downloaded the **google-services.json** file, you can do download it on this page.
4034

41-
1. Switch to the **Cloud Messaging** tab at the top.
35+
![Download google-services.json from the General tab](./media/notification-hubs-enable-firebase-cloud-messaging/download-google-services-json-general-page.png)
36+
1. Switch to the **Cloud Messaging** tab at the top. Copy and save the **Server key** for later use. You use this value to configure your notification hub.
4237

43-
1. Copy and save the **Legacy Server key** for later use. You use this value to configure your notification hub.
38+
![Copy server key](./media/notification-hubs-enable-firebase-cloud-messaging/server-key.png)

0 commit comments

Comments
 (0)