Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 578ebf8

Browse files
committed
More retired docs.
1 parent 187c433 commit 578ebf8

File tree

274 files changed

+43916
-15442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

274 files changed

+43916
-15442
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace NotificationDispatcher
2+
{
3+
public static class DispatcherConstants
4+
{
5+
6+
/// <summary>
7+
/// These are the tags the app will send to. These could be subjects like
8+
/// news, sports, and weather, or they can be custom tags that identify a user
9+
/// across devices.
10+
/// </summary>
11+
public static string[] SubscriptionTags { get; set; } = { "default" };
12+
13+
14+
/// <summary>
15+
/// This is the name of your Azure Notification Hub, found in your Azure portal.
16+
/// </summary>
17+
public static string NotificationHubName { get; set; } = "< Insert your Azure Notification Hub name >";
18+
19+
/// <summary>
20+
/// This is the "DefaultFullSharedAccessSignature" connection string, which is
21+
/// found in your Azure Notification Hub portal under "Access Policies".
22+
/// </summary>
23+
public static string FullAccessConnectionString { get; set; } = "< Insert your DefaultFullSharedAccessSignature >";
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="3.1.0" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.Azure.NotificationHubs;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace NotificationDispatcher
7+
{
8+
class Program
9+
{
10+
static int messageCount = 0;
11+
12+
static void Main(string[] args)
13+
{
14+
Console.WriteLine($"Press the spacebar to send a message to each tag in {string.Join(", ", DispatcherConstants.SubscriptionTags)}");
15+
WriteSeparator();
16+
while(Console.ReadKey().Key == ConsoleKey.Spacebar)
17+
{
18+
SendTemplateNotificationsAsync().GetAwaiter().GetResult();
19+
}
20+
}
21+
22+
private static async Task SendTemplateNotificationsAsync()
23+
{
24+
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(DispatcherConstants.FullAccessConnectionString, DispatcherConstants.NotificationHubName);
25+
Dictionary<string, string> templateParameters = new Dictionary<string, string>();
26+
27+
messageCount++;
28+
29+
// Send a template notification to each tag. This will go to any devices that
30+
// have subscribed to this tag with a template that includes "messageParam"
31+
// as a parameter
32+
foreach (var tag in DispatcherConstants.SubscriptionTags)
33+
{
34+
templateParameters["messageParam"] = $"Notification #{messageCount} to {tag} category subscribers!";
35+
try
36+
{
37+
await hub.SendTemplateNotificationAsync(templateParameters, tag);
38+
Console.WriteLine($"Sent message to {tag} subscribers.");
39+
}
40+
catch(Exception ex)
41+
{
42+
Console.WriteLine($"Failed to send template notification: {ex.Message}");
43+
}
44+
}
45+
46+
Console.WriteLine($"Sent messages to {DispatcherConstants.SubscriptionTags.Length} tags.");
47+
WriteSeparator();
48+
}
49+
50+
private static void WriteSeparator()
51+
{
52+
Console.WriteLine("==========================================================================");
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28803.352
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NotificationHubSample.Android", "NotificationHubSample\NotificationHubSample.Android\NotificationHubSample.Android.csproj", "{92198748-B83C-4393-8AC8-EF6978BD7FDC}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NotificationHubSample.iOS", "NotificationHubSample\NotificationHubSample.iOS\NotificationHubSample.iOS.csproj", "{7742CEBC-3E9F-4280-B701-20DA7580B5DF}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NotificationHubSample", "NotificationHubSample\NotificationHubSample\NotificationHubSample.csproj", "{3566DAFB-7C41-4882-A7D9-600FF02EF624}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NotificationDispatcher", "NotificationDispatcher\NotificationDispatcher.csproj", "{21D10D97-8583-4D8E-986A-EFC9051075E6}"
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Debug|iPhone = Debug|iPhone
18+
Debug|iPhoneSimulator = Debug|iPhoneSimulator
19+
Release|Any CPU = Release|Any CPU
20+
Release|iPhone = Release|iPhone
21+
Release|iPhoneSimulator = Release|iPhoneSimulator
22+
EndGlobalSection
23+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
24+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
27+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|iPhone.ActiveCfg = Debug|Any CPU
28+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|iPhone.Build.0 = Debug|Any CPU
29+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|iPhone.Deploy.0 = Debug|Any CPU
30+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
31+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
32+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Debug|iPhoneSimulator.Deploy.0 = Debug|Any CPU
33+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|Any CPU.Deploy.0 = Release|Any CPU
36+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|iPhone.ActiveCfg = Release|Any CPU
37+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|iPhone.Build.0 = Release|Any CPU
38+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|iPhone.Deploy.0 = Release|Any CPU
39+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
40+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
41+
{92198748-B83C-4393-8AC8-EF6978BD7FDC}.Release|iPhoneSimulator.Deploy.0 = Release|Any CPU
42+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator
43+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator
44+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|Any CPU.Deploy.0 = Debug|iPhoneSimulator
45+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|iPhone.ActiveCfg = Debug|iPhone
46+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|iPhone.Build.0 = Debug|iPhone
47+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|iPhone.Deploy.0 = Debug|iPhone
48+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
49+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
50+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Debug|iPhoneSimulator.Deploy.0 = Debug|iPhoneSimulator
51+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|Any CPU.ActiveCfg = Release|iPhone
52+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|Any CPU.Build.0 = Release|iPhone
53+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|Any CPU.Deploy.0 = Release|iPhone
54+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|iPhone.ActiveCfg = Release|iPhone
55+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|iPhone.Build.0 = Release|iPhone
56+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|iPhone.Deploy.0 = Release|iPhone
57+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
58+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
59+
{7742CEBC-3E9F-4280-B701-20DA7580B5DF}.Release|iPhoneSimulator.Deploy.0 = Release|iPhoneSimulator
60+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
63+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|iPhone.ActiveCfg = Debug|Any CPU
64+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|iPhone.Build.0 = Debug|Any CPU
65+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|iPhone.Deploy.0 = Debug|Any CPU
66+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
67+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
68+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Debug|iPhoneSimulator.Deploy.0 = Debug|Any CPU
69+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|Any CPU.ActiveCfg = Release|Any CPU
70+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|Any CPU.Build.0 = Release|Any CPU
71+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|Any CPU.Deploy.0 = Release|Any CPU
72+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|iPhone.ActiveCfg = Release|Any CPU
73+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|iPhone.Build.0 = Release|Any CPU
74+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|iPhone.Deploy.0 = Release|Any CPU
75+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
76+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
77+
{3566DAFB-7C41-4882-A7D9-600FF02EF624}.Release|iPhoneSimulator.Deploy.0 = Release|Any CPU
78+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
80+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Debug|iPhone.ActiveCfg = Debug|Any CPU
81+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Debug|iPhone.Build.0 = Debug|Any CPU
82+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
83+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
84+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
85+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Release|iPhone.ActiveCfg = Release|Any CPU
87+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Release|iPhone.Build.0 = Release|Any CPU
88+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
89+
{21D10D97-8583-4D8E-986A-EFC9051075E6}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
90+
EndGlobalSection
91+
GlobalSection(SolutionProperties) = preSolution
92+
HideSolutionNode = FALSE
93+
EndGlobalSection
94+
GlobalSection(ExtensibilityGlobals) = postSolution
95+
SolutionGuid = {45897760-34A1-4989-95B5-4EDB2A96BD5B}
96+
EndGlobalSection
97+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Android.App;
2+
using Android.Content;
3+
using Android.OS;
4+
using Android.Support.V4.App;
5+
using Android.Util;
6+
using Firebase.Messaging;
7+
using System;
8+
using System.Linq;
9+
using WindowsAzure.Messaging;
10+
11+
namespace NotificationHubSample.Droid
12+
{
13+
[Service]
14+
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
15+
public class FirebaseService : FirebaseMessagingService
16+
{
17+
public override void OnMessageReceived(RemoteMessage message)
18+
{
19+
base.OnMessageReceived(message);
20+
string messageBody = string.Empty;
21+
22+
if (message.GetNotification() != null)
23+
{
24+
messageBody = message.GetNotification().Body;
25+
}
26+
27+
// NOTE: test messages sent via the Azure portal will be received here
28+
else
29+
{
30+
messageBody = message.Data.Values.First();
31+
}
32+
33+
// convert the incoming message to a local notification
34+
SendLocalNotification(messageBody);
35+
36+
// send the incoming message directly to the MainPage
37+
SendMessageToMainPage(messageBody);
38+
}
39+
40+
public override void OnNewToken(string token)
41+
{
42+
// TODO: save token instance locally, or log if desired
43+
44+
SendRegistrationToServer(token);
45+
}
46+
47+
void SendLocalNotification(string body)
48+
{
49+
var intent = new Intent(this, typeof(MainActivity));
50+
intent.AddFlags(ActivityFlags.ClearTop);
51+
intent.PutExtra("message", body);
52+
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
53+
54+
var notificationBuilder = new NotificationCompat.Builder(this, AppConstants.NotificationChannelName)
55+
.SetContentTitle("XamarinNotify Message")
56+
.SetSmallIcon(Resource.Drawable.ic_launcher)
57+
.SetContentText(body)
58+
.SetAutoCancel(true)
59+
.SetShowWhen(false)
60+
.SetContentIntent(pendingIntent);
61+
62+
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
63+
{
64+
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
65+
}
66+
67+
var notificationManager = NotificationManager.FromContext(this);
68+
notificationManager.Notify(0, notificationBuilder.Build());
69+
}
70+
71+
void SendMessageToMainPage(string body)
72+
{
73+
(App.Current.MainPage as MainPage)?.AddMessage(body);
74+
}
75+
76+
void SendRegistrationToServer(string token)
77+
{
78+
try
79+
{
80+
NotificationHub hub = new NotificationHub(AppConstants.NotificationHubName, AppConstants.ListenConnectionString, this);
81+
82+
// register device with Azure Notification Hub using the token from FCM
83+
Registration registration = hub.Register(token, AppConstants.SubscriptionTags);
84+
85+
// subscribe to the SubscriptionTags list with a simple template.
86+
string pnsHandle = registration.PNSHandle;
87+
TemplateRegistration templateReg = hub.RegisterTemplate(pnsHandle, "defaultTemplate", AppConstants.FCMTemplateBody, AppConstants.SubscriptionTags);
88+
}
89+
catch (Exception e)
90+
{
91+
Log.Error(AppConstants.DebugTag, $"Error registering device: {e.Message}");
92+
}
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Android.App;
2+
using Android.Content;
3+
using Android.Content.PM;
4+
using Android.Gms.Common;
5+
using Android.OS;
6+
using Android.Util;
7+
using System;
8+
9+
namespace NotificationHubSample.Droid
10+
{
11+
[Activity(Label = "NotificationHubSample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
12+
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
13+
{
14+
protected override void OnCreate(Bundle savedInstanceState)
15+
{
16+
base.OnCreate(savedInstanceState);
17+
18+
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
19+
LoadApplication(new App());
20+
21+
if (IsPlayServiceAvailable() == false)
22+
{
23+
throw new Exception("This device does not have Google Play Services and cannot receive push notifications.");
24+
}
25+
26+
CreateNotificationChannel();
27+
}
28+
29+
protected override void OnNewIntent(Intent intent)
30+
{
31+
if (intent.Extras != null)
32+
{
33+
var message = intent.GetStringExtra("message");
34+
(App.Current.MainPage as MainPage)?.AddMessage(message);
35+
}
36+
37+
base.OnNewIntent(intent);
38+
}
39+
40+
bool IsPlayServiceAvailable()
41+
{
42+
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
43+
if (resultCode != ConnectionResult.Success)
44+
{
45+
if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
46+
Log.Debug(AppConstants.DebugTag, GoogleApiAvailability.Instance.GetErrorString(resultCode));
47+
else
48+
{
49+
Log.Debug(AppConstants.DebugTag, "This device is not supported");
50+
}
51+
return false;
52+
}
53+
return true;
54+
}
55+
56+
void CreateNotificationChannel()
57+
{
58+
// Notification channels are new as of "Oreo".
59+
// There is no need to create a notification channel on older versions of Android.
60+
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
61+
{
62+
var channelName = AppConstants.NotificationChannelName;
63+
var channelDescription = String.Empty;
64+
var channel = new NotificationChannel(channelName, channelName, NotificationImportance.Default)
65+
{
66+
Description = channelDescription
67+
};
68+
69+
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
70+
notificationManager.CreateNotificationChannel(channel);
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)