Christophe
Christophe

Reputation: 46

Android AlarmManager (sometimes) provides extra notification

I am working on an application that uses the AlarmManager to generate a notification for the user at a later date. The Alarm always arrives (on the devices tested) but one device (Samsung Galaxy Tab3, Android 4.4.2) receives 2 notifications (presumably one from the alarmManager, and one from the broadcast receiver of the app). The code for both is of course the same, and I haven't seen this issue before.Nexus 5 Galaxy Tab

The code is written in C# for Xamarin, but it is pretty vanilla. The AlarmManager creates a broadcast with a broadcast filter that will be picked up elsewhere in the app (and that works fine):

private PendingIntent CreateIntent (Models.Notifications.LocalNotificationModel notification, PendingIntentFlags flag) { if (notification.IsActive) { //Create the broadcast intent var broadcastIntent = new Intent (BROADCASTFILTER);

            //Add information to the receiver of what should be done about the notification
            var id = notification.Id;
            var message = notification.Message;
            var badgeNumber = notification.BadgeNumber;
            var dateOfEvent = notification.EventDateTime;
            var iconPath = notification.IconPath;
            var isActive = notification.IsActive;
            var isOverride = notification.IsOverride;

            //Needs extra information to create the notification
            broadcastIntent.PutExtra (ID, id);
            broadcastIntent.PutExtra (MESSAGE, message);
            broadcastIntent.PutExtra (DATEOFEVENT, dateOfEvent.ToShortDateString () + " " + dateOfEvent.ToShortTimeString ());

            //Get the broadcast as pending intent
            var source = PendingIntent.GetBroadcast (_context, (int)id, broadcastIntent, flag);

            return source;
        }
        return null;
    }

    public void Add(Models.Notifications.LocalNotificationModel notification)
    {
        if (!_isInitialized)
            throw new ApplicationException("LocalNotificationService is not initialized");

        var source = CreateIntent (notification,PendingIntentFlags.OneShot);

        //Convert the notification to milliseconds since epoch
        var sinceEpoch = notification.EventDateTime.ToUniversalTime() - new DateTime(1970, 1, 1);
        var msSinceEpoch = (long)sinceEpoch.TotalMilliseconds;

        //Assign the pending intent to the alarmmanager
        _alarmManager.Set(AlarmType.RtcWakeup, msSinceEpoch, source);
    }

Upvotes: 1

Views: 268

Answers (1)

Christophe
Christophe

Reputation: 46

It was an error on my part, there was a proof of concept version of the application on the tablet, it was a different app but used the same bundle ID, and as a result received the same broadcast.

Upvotes: 0

Related Questions