Skip to content

Commit 1c8dbb7

Browse files
Merge pull request #77835 from jwargo/jmw-fix-gcm-links
Fixed based on https://github.com/MicrosoftDocs/azure-docs/issues/32100
2 parents 97d94fd + 6c5e3ee commit 1c8dbb7

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

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

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,26 @@ Your notification hub is configured to work with FCM, and you have the connectio
6262

6363
### Create Visual Studio project and add NuGet packages
6464

65-
1. In Visual Studio, open the **File** menu, select **New**, and then select **Project**. In the **New Project** window, do these steps:
65+
1. In Visual Studio, open the **File** menu, select **New**, and then select **Project**. In the **New Project** window, do these steps:
6666
1. Expand **Installed**, **Visual C#**, and then click **Android**.
67-
2. Select **Android App (Xamarin)** from the list.
68-
3. Enter a **name** for the project.
69-
4. Select a **location** for the project.
70-
5. Select **OK**
67+
2. Select **Android App (Xamarin)** from the list.
68+
3. Enter a **name** for the project.
69+
4. Select a **location** for the project.
70+
5. Select **OK**
7171

72-
![New Project dialog](./media/partner-xamarin-notification-hubs-android-get-started/new-project-dialog-new.png)
73-
2. On the **New Android App** dialog box, select **Blank App**, and select **OK**.
72+
![New Project dialog](./media/partner-xamarin-notification-hubs-android-get-started/new-project-dialog-new.png)
73+
2. On the **New Android App** dialog box, select **Blank App**, and select **OK**.
7474

7575
![New Project dialog](./media/partner-xamarin-notification-hubs-android-get-started/new-android-app-dialog.png)
76-
1. In the **Solution Explorer** window, expand **Properties**, and click **AndroidManifest.xml**. Update the package name to match the package name you entered when adding Firebase Cloud Messaging to your project in the Google Firebase Console.
76+
3. In the **Solution Explorer** window, expand **Properties**, and click **AndroidManifest.xml**. Update the package name to match the package name you entered when adding Firebase Cloud Messaging to your project in the Google Firebase Console.
7777

7878
![Package name in GCM](./media/partner-xamarin-notification-hubs-android-get-started/package-name-gcm.png)
79-
3. Right-click your project, and select **Manage NuGet Packages...**.
80-
4. Select the **Browse** tab. Search for **Xamarin.GooglePlayServices.Base**. Select **Xamarin.GooglePlayServices.Base** in the result list. Then, select **Install**.
79+
4. Right-click your project, and select **Manage NuGet Packages...**.
80+
5. Select the **Browse** tab. Search for **Xamarin.GooglePlayServices.Base**. Select **Xamarin.GooglePlayServices.Base** in the result list. Then, select **Install**.
8181

8282
![Google Play Services NuGet](./media/partner-xamarin-notification-hubs-android-get-started/google-play-services-nuget.png)
83-
5. In the **NuGet Package Manager** window, search for **Xamarin.Firebase.Messaging**. Select **Xamarin.Firebase.Messaging** in the result list. Then, select **Install**.
84-
6. Now, search for **Xamarin.Azure.NotificationHubs.Android**. Select **Xamarin.Azure.NotificationHubs.Android** in the result list. Then, select **Install**.
83+
6. In the **NuGet Package Manager** window, search for **Xamarin.Firebase.Messaging**. Select **Xamarin.Firebase.Messaging** in the result list. Then, select **Install**.
84+
7. Now, search for **Xamarin.Azure.NotificationHubs.Android**. Select **Xamarin.Azure.NotificationHubs.Android** in the result list. Then, select **Install**.
8585

8686
### Add the Google Services JSON File
8787

@@ -108,20 +108,22 @@ Your notification hub is configured to work with FCM, and you have the connectio
108108
</intent-filter>
109109
</receiver>
110110
```
111-
2. Add the following statements **before the application** element.
111+
112+
2. Add the following statements **before the application** element.
112113

113114
```xml
114115
<uses-permission android:name="android.permission.INTERNET" />
115116
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
116117
<uses-permission android:name="android.permission.WAKE_LOCK" />
117118
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
118119
```
119-
1. Gather the following information for your Android app and notification hub:
120+
121+
3. Gather the following information for your Android app and notification hub:
120122

121123
* **Listen connection string**: On the dashboard in the [Azure portal], choose **View connection strings**. Copy the `DefaultListenSharedAccessSignature` connection string for this value.
122124
* **Hub name**: Name of your hub from the [Azure portal]. For example, *mynotificationhub2*.
123-
3. In the **Solution Explorer** window, right-click your **project**, select **Add**, and then select **Class**.
124-
4. Create a `Constants.cs` class for your Xamarin project and define the following constant values in the class. Replace the placeholders with your values.
125+
4. In the **Solution Explorer** window, right-click your **project**, select **Add**, and then select **Class**.
126+
5. Create a `Constants.cs` class for your Xamarin project and define the following constant values in the class. Replace the placeholders with your values.
125127

126128
```csharp
127129
public static class Constants
@@ -130,19 +132,22 @@ Your notification hub is configured to work with FCM, and you have the connectio
130132
public const string NotificationHubName = "<hub name>";
131133
}
132134
```
133-
5. Add the following using statements to `MainActivity.cs`:
135+
136+
6. Add the following using statements to `MainActivity.cs`:
134137

135138
```csharp
136139
using Android.Util;
137140
using Android.Gms.Common;
138141
```
139-
6. Add the following properties to the MainActivity class. The TAG variable will be used to show an alert dialog when the app is running:
142+
143+
7. Add the following properties to the MainActivity class. The TAG variable will be used to show an alert dialog when the app is running:
140144

141145
```csharp
142146
public const string TAG = "MainActivity";
143147
internal static readonly string CHANNEL_ID = "my_notification_channel";
144148
```
145-
7. Add the following method to the MainActivity class. It checks whether **Google Play Services** are available on the device.
149+
150+
8. Add the following method to the MainActivity class. It checks whether **Google Play Services** are available on the device.
146151

147152
```csharp
148153
public bool IsPlayServicesAvailable()
@@ -159,12 +164,13 @@ Your notification hub is configured to work with FCM, and you have the connectio
159164
}
160165
return false;
161166
}
162-
167+
163168
Log.Debug(TAG, "Google Play Services is available.");
164169
return true;
165170
}
166171
```
167-
1. Add the following method to the MainActivity class that creates a notification channel.
172+
173+
9. Add the following method to the MainActivity class that creates a notification channel.
168174

169175
```csharp
170176
private void CreateNotificationChannel()
@@ -176,19 +182,20 @@ Your notification hub is configured to work with FCM, and you have the connectio
176182
// channel on older versions of Android.
177183
return;
178184
}
179-
185+
180186
var channelName = CHANNEL_ID;
181187
var channelDescription = string.Empty;
182188
var channel = new NotificationChannel(CHANNEL_ID, channelName, NotificationImportance.Default)
183189
{
184190
Description = channelDescription
185191
};
186-
192+
187193
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
188194
notificationManager.CreateNotificationChannel(channel);
189195
}
190196
```
191-
1. In `MainActivity.cs`, add the following code to `OnCreate` after `base.OnCreate(savedInstanceState)`:
197+
198+
10. In `MainActivity.cs`, add the following code to `OnCreate` after `base.OnCreate(savedInstanceState)`:
192199

193200
```csharp
194201
if (Intent.Extras != null)
@@ -202,27 +209,29 @@ Your notification hub is configured to work with FCM, and you have the connectio
202209
}
203210
}
204211
}
205-
212+
206213
IsPlayServicesAvailable();
207214
CreateNotificationChannel();
208215
```
209-
8. Create a new class, `MyFirebaseIIDService` like you created the `Constants` class.
210-
9. Add the following using statements to `MyFirebaseIIDService.cs`:
216+
217+
11. Create a new class, `MyFirebaseIIDService` like you created the `Constants` class.
218+
12. Add the following using statements to `MyFirebaseIIDService.cs`:
211219

212220
```csharp
213221
using Android.Util;
214222
using WindowsAzure.Messaging;
215223
using Firebase.Iid;
216224
```
217225

218-
10. In `MyFirebaseIIDService.cs`, add the following `class` declaration, and have your class inherit from `FirebaseInstanceIdService`:
226+
13. In `MyFirebaseIIDService.cs`, add the following `class` declaration, and have your class inherit from `FirebaseInstanceIdService`:
219227

220228
```csharp
221229
[Service]
222230
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
223231
public class MyFirebaseIIDService : FirebaseInstanceIdService
224232
```
225-
11. In `MyFirebaseIIDService.cs`, add the following code:
233+
234+
14. In `MyFirebaseIIDService.cs`, add the following code:
226235

227236
```csharp
228237
const string TAG = "MyFirebaseIIDService";
@@ -247,23 +256,26 @@ Your notification hub is configured to work with FCM, and you have the connectio
247256
Log.Debug(TAG, $"Successful registration of ID {regID}");
248257
}
249258
```
250-
12. Create another new class for your project, name it `MyFirebaseMessagingService`.
251-
13. Add the following using statements to `MyFirebaseMessagingService.cs`.
259+
260+
15. Create another new class for your project, name it `MyFirebaseMessagingService`.
261+
16. Add the following using statements to `MyFirebaseMessagingService.cs`.
252262

253263
```csharp
254264
using Android.Util;
255265
using Firebase.Messaging;
256266
using Android.Support.V4.App;
257267
using Build = Android.OS.Build;
258268
```
259-
14. Add the following above your class declaration, and have your class inherit from `FirebaseMessagingService`:
269+
270+
17. Add the following above your class declaration, and have your class inherit from `FirebaseMessagingService`:
260271

261272
```csharp
262273
[Service]
263274
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
264275
public class MyFirebaseMessagingService : FirebaseMessagingService
265276
```
266-
15. Add the following code to `MyFirebaseMessagingService.cs`:
277+
278+
18. Add the following code to `MyFirebaseMessagingService.cs`:
267279

268280
```csharp
269281
const string TAG = "MyFirebaseMsgService";
@@ -308,8 +320,9 @@ Your notification hub is configured to work with FCM, and you have the connectio
308320
notificationManager.Notify(0, notificationBuilder.Build());
309321
}
310322
```
311-
16. **Build** your project.
312-
17. **Run** your app on your device or loaded emulator
323+
324+
19. **Build** your project.
325+
20. **Run** your app on your device or loaded emulator
313326

314327
## Send test notification from the Azure portal
315328

@@ -324,7 +337,7 @@ Push notifications are normally sent in a back-end service like Mobile Services
324337
In this tutorial, you sent broadcast notifications to all your Android devices registered with the backend. To learn how to push notifications to specific Android devices, advance to the following tutorial:
325338

326339
> [!div class="nextstepaction"]
327-
>[Push notifications to specific devices](notification-hubs-aspnet-backend-android-xplat-segmented-gcm-push-notification.md)
340+
>[Push notifications to specific devices](push-notifications-android-specific-devices-firebase-cloud-messaging.md)
328341

329342
<!-- Anchors. -->
330343
[Enable Google Cloud Messaging]: #register

0 commit comments

Comments
 (0)