-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.cs
104 lines (90 loc) · 4.46 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Xamarin.Forms;
namespace RoosterBlock.Droid
{
// TODO: The SingleTop mode prevents multiple instances of an Activity from being started while the application is in the foreground. This LaunchMode may
// not be appropriate for applications that launch multiple activities in more complex notification scenarios. For more information about LaunchMode
// enumeration values, see Android Activity LaunchMode. *** The LaunchMode.SingleTop may not be the right mode for this app. ***
// https://developer.android.com/guide/topics/manifest/activity-element#lmode
[Activity(
Label = "Rooster Block",
Icon = "@mipmap/icon",
Theme = "@style/MainTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize,
LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
MMSReceiver mmsReceiver;
SMSReceiver smsReceiver;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
mmsReceiver = new MMSReceiver();
smsReceiver = new SMSReceiver();
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
Log.Info("MainActivity", "Rob Completed Initialization");
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
/// <summary>
/// If the application is already running in the foreground, the Intent data will be passed to this OnNewIntent
/// method. Note that when the application is started by notification data, the Intent data will be passed to
/// the OnCreate method.
/// </summary>
/// <param name="intent">This contains the title and message of the notification.</param>
protected override void OnNewIntent(Intent intent)
{
CreateNotificationFromIntent(intent);
}
/// <summary>
/// Create the Android notification when a message is received.
/// </summary>
/// <param name="intent">This contains the title and message of the notification.</param>
public static void CreateNotificationFromIntent(Intent intent)
{
// TODO: Android offers many advanced options for notifications. For more information, see Notifications in
// Xamarin.Android.
// https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/
if (intent?.Extras != null)
{
string title = intent.Extras.GetString(AndroidNotificationManager.TitleKey);
string message = intent.Extras.GetString(AndroidNotificationManager.MessageKey);
DependencyService.Get<INotificationManager>().ReceiveNotification(title, message);
}
}
protected override void OnResume()
{
base.OnResume();
IntentFilter smsIntentfilter = new IntentFilter(Android.Provider.Telephony.Sms.Intents.SmsReceivedAction)
{
Priority = (int)IntentFilterPriority.HighPriority
};
IntentFilter mmsIntentfilter = new IntentFilter(Android.Provider.Telephony.Sms.Intents.WapPushReceivedAction)
{
Priority = (int)IntentFilterPriority.HighPriority
};
mmsIntentfilter.AddDataType("application/vnd.wap.mms-message");
RegisterReceiver(smsReceiver, smsIntentfilter);
RegisterReceiver(mmsReceiver, mmsIntentfilter);
}
protected override void OnDestroy()
{
UnregisterReceiver(mmsReceiver);
UnregisterReceiver(smsReceiver);
base.OnDestroy();
}
}
}