-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced services with Jobs for Android notifications and fixed other…
… errors
- Loading branch information
Noc2
committed
Apr 10, 2018
1 parent
cf999fa
commit 333efc2
Showing
23 changed files
with
359 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Chiota.Droid.Services | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
using Android.App; | ||
using Android.Content; | ||
using Android.Media; | ||
using Android.OS; | ||
using Android.Support.V4.App; | ||
|
||
using Chiota.Models; | ||
using Chiota.Services; | ||
using Java.Lang; | ||
|
||
using Resource = Resource; | ||
|
||
public class NotificationsTask : AsyncTask<Void, Void, Task<bool>> | ||
{ | ||
protected override async Task<bool> RunInBackground(params Void[] @params) | ||
{ | ||
var finished = await this.LookForNewNotifications(); | ||
return finished; | ||
} | ||
|
||
private async Task<bool> LookForNewNotifications() | ||
{ | ||
// seed needs to be stored on device!! | ||
var secureStorage = new SecureStorage(); | ||
if (secureStorage.CheckUserStored()) | ||
{ | ||
var user = await secureStorage.GetUser(); | ||
if (user != null) | ||
{ | ||
var contactApprovedList = await user.TangleMessenger.GetJsonMessageAsync<SentDataWrapper<Contact>>(user.ApprovedAddress); | ||
|
||
// currently no messages for contact request due to perfomance issues | ||
foreach (var contact in contactApprovedList.Where(c => !c.Data.Rejected)) | ||
{ | ||
var encryptedMessages = await user.TangleMessenger.GetMessagesAsync(contact.Data.ChatAdress); | ||
|
||
foreach (var unused in encryptedMessages.Where(c => !c.Stored)) | ||
{ | ||
var builder = new NotificationCompat.Builder(Application.Context) | ||
.SetAutoCancel(true) // Dismiss from the notif. area when clicked | ||
.SetContentTitle(contact.Data.Name) // Set its title | ||
.SetContentText("New Message from " + contact.Data.Name) | ||
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)) | ||
.SetSmallIcon(Resource.Drawable.reminder); | ||
var notification = builder.Build(); | ||
var notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager; | ||
notificationManager?.Notify(0, notification); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace Chiota.Droid.Services | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Android.App; | ||
using Android.App.Job; | ||
using Android.OS; | ||
|
||
using Chiota.Messages; | ||
|
||
using Xamarin.Forms; | ||
|
||
[Service(Name = "ChiotaApp.ChiotaApp.PeriodicJob", Permission = "android.permission.BIND_JOB_SERVICE")] | ||
public class PeriodicJob : JobService | ||
{ | ||
private CancellationTokenSource cts; | ||
|
||
public override bool OnStartJob(JobParameters jobParameters) | ||
{ | ||
// Called by the operating system when starting the service. | ||
// Start up a thread, do work on the thread. | ||
this.cts = new CancellationTokenSource(); | ||
|
||
Task.Run( | ||
() => | ||
{ | ||
try | ||
{ | ||
var notification = new NotificationsTask(); | ||
notification.Execute(); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
finally | ||
{ | ||
if (this.cts.IsCancellationRequested) | ||
{ | ||
var message = new CancelledMessage(); | ||
Device.BeginInvokeOnMainThread(() => MessagingCenter.Send(message, "CancelledMessage")); | ||
} | ||
} | ||
}, | ||
this.cts.Token); | ||
|
||
return true; | ||
} | ||
|
||
public override bool OnStopJob(JobParameters jobParameters) | ||
{ | ||
// Called by Android when it has to terminate a running service. | ||
if (this.cts != null) | ||
{ | ||
this.cts.Token.ThrowIfCancellationRequested(); | ||
|
||
this.cts.Cancel(); | ||
} | ||
|
||
return true; // false don't reschedule the job. | ||
} | ||
} | ||
} |
Oops, something went wrong.