localnotificationsplugin icon indicating copy to clipboard operation
localnotificationsplugin copied to clipboard

Any suggestions how to save pending notifications?

Open rubenc57 opened this issue 8 years ago • 7 comments

Hello you said: "Android: if the phone is re-booted then the pending notifications are not sent, you should save them out to settings and re-send on re-boot."

Any idea how to do this?

rubenc57 avatar Nov 08 '17 19:11 rubenc57

@edsnider i would like to know that too :) @jamesmontemagno @JensSchadron please assist :)

CDrosos avatar Apr 21 '18 01:04 CDrosos

Quick overview of what I did for this. I used a local sqlite database where prior to sending the notifications, I added an entry to a local notifications table containing the message, title, id, etc. of the notification that was being sent.

To access this on device start, in the Android project there are a few things to do.

  1. Add the following int the AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/icon">
    <receiver android:enabled="true" 
                    android:name=".BootUpReceiver"
                    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
</application>`
  1. Create your BootUpReceiver in a new file to handle resending the notifications.
using Android.App;
using Android.Content;
using MyApp;
using Plugin.LocalNotifications;

[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class BootUpReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == Intent.ActionBootCompleted)
        {
	    // Set up notification icon if necessary
            LocalNotificationsImplementation.NotificationIconId = CTK.Mobile.Droid.Resource.Drawable.ic_notification;

            // NotificationService is where you will retrieve your notifications from the sqlite db and cancel/resend them.
	    NotificationService.ResendNotifications();
        }
    }
}

It would be awesome if this could be built into the package.

finnsea avatar Jun 04 '18 17:06 finnsea

@finnsea thanks for the comment. i have try your implementation and i have change the onrecieve to match my app:

```
public override void OnReceive(Context context, Intent intent)
{
    if (intent.Action == Intent.ActionBootCompleted)
    {
        if (Settings.TaskReminderList != string.Empty)
            CurrentTaskReminders = JsonConvert.DeserializeObject<List<TaskReminder>>(Settings.TaskReminderList);

        // Set up notification icon if necessary
        var resourceId = (int)typeof(life_is_amazing.Resource.Drawable).GetField("ic_launcher").GetValue(null);
        LocalNotificationsImplementation.NotificationIconId = resourceId;

        // Retrieve notifications and cancel/resend them.
        if (CurrentTaskReminders.Count() > 0)
        {
            for (int i = 0; i < CurrentTaskReminders.Count(); i++)
            {
                CrossLocalNotifications.Current.Show(GetString(life_is_amazing.Resource.String.SetReminders_ReminderTitle), GetString(life_is_amazing.Resource.String.SetReminders_ReminderMessage), CurrentTaskReminders[i].CrossLocalNotificationID, CurrentTaskReminders[i].Date);
            }
        }
    }
}

but it doesnt work. i target 8.1 android and i have test it on android 6. im not sure if im making any mistake. im getting my list of notifications from sharedpreferences

CDrosos avatar Jun 05 '18 20:06 CDrosos

@CDrosos as you have probably found out by now, according to the docs

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically). You can still use a context-registered reciever when the user is actively using your app.

UnreachableCode avatar Jul 24 '18 11:07 UnreachableCode

Im not sure what that means. So if i target android 8 how i can send a norificafion after phone restart and without the user to open first the app? Or your comment means that it is not possible?  Στάλθηκε από το Ταχυδρομείο Yahoo σε Android

Στις Τρί, 24 Ιουλ, 2018 στις 14:20, ο χρήστηςCharlie Finlayson[email protected] έγραψε:
@CDrosos as you have probably found out by now, according to the docs

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically). You can still use a context-registered reciever when the user is actively using your app.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

CDrosos avatar Jul 24 '18 11:07 CDrosos

At the moment im working on the job scheduler, you can find the pr here #42 One problem is, that i can´t get it to work after reboot curse permission things.

DoktorAerzt avatar Jul 24 '18 12:07 DoktorAerzt

According to docs: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead. Some broadcast are still working like on boot conpleted so the code of the user finnsea shouldn't still work?

CDrosos avatar Jul 26 '18 04:07 CDrosos