Android Open Source - Multi-Mania-app Notification Sender






From Project

Back to project page Multi-Mania-app.

License

The source code is released under:

MIT License

If you think the Android project Multi-Mania-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package be.ana.nmct.multimania.service;
/*www  .jav a2  s . c  o m*/
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

import java.util.List;

import be.ana.nmct.multimania.R;
import be.ana.nmct.multimania.data.MultimaniaContract;
import be.ana.nmct.multimania.ui.TalkActivity;
import be.ana.nmct.multimania.utils.Utility;
import be.ana.nmct.multimania.vm.ScheduleTalkVm;

/**
 * Created by Axel on 18/11/2014.
 */

/**
 * This class handles sending the sending of notifications as well as planning when notifications
 * should be send to the user.
 */
public class NotificationSender {

    private static final String TAG = NotificationSender.class.getSimpleName();

    public final static int NOTIFICATION_ID = 12345;
    public final static String NOTIF_TALKID = "notification_talkid";
    public final static String INTENT_ALARMRECEIVER = "be.ana.nmct.multimania.ALARMRECEIVER";

    private static Context sContext;

    public NotificationSender(Context context) {
        this.sContext = context;
    }

    /**
     * Sets an alarm for a bunch of talks
     * @param talks The List<Talk> we need to set the alarms for
     */
    public void setAlarmForTalkList(List<ScheduleTalkVm> talks){
        for(ScheduleTalkVm talk : talks){
            setAlarmForTalk(talk);
        }
    }

    /**
     * Sets an alarm to trigger a notification for a Talk
     * @param talk The talk we need to set the alarm for
     */
    public void setAlarmForTalk(ScheduleTalkVm talk){
        Intent intent = new Intent(sContext, NotificationReceiver.class);
        intent.putExtra(NOTIF_TALKID, talk.id);
        intent.setAction(INTENT_ALARMRECEIVER);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(sContext, (int)talk.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager)sContext.getSystemService(Context.ALARM_SERVICE);
        long millis = Utility.getDateInMillis(talk.from) - 600000;      //600.000 = 10 minutes
        if(millis > 0){
            alarmManager.set(AlarmManager.RTC_WAKEUP, millis, pendingIntent);
        }

    }

    /**
     * Cancels a bunch of alarms for each Talk in talks
     * @param talks The List<Talk> we need to cancel the alarms for
     */
    public void cancelAlarmForTalkList(List<ScheduleTalkVm> talks){
        for(ScheduleTalkVm talk : talks){
            cancelAlarmForTalk(talk);
        }
    }

    /**
     * Cancels an alarm set to trigger a notification
     * @param talk The Talk we need to cancel the alarm for
     */
    public void cancelAlarmForTalk(ScheduleTalkVm talk){
        Intent intent = new Intent(sContext, NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(sContext, (int)talk.id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager)sContext.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }

    /**
     * Sends a notification containing a reminder for the user
     * @param context The context (e.g. an Activity)
     * @param talk The Talk we need to make a notification for
     */
    public static void sendNotification(Context context, ScheduleTalkVm talk){

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_notification)
                        .setContentTitle(talk.title)
                        .setContentText(context.getString(R.string.notification_text))
                        .setSound(getNotificationSoundUri())
                        .setLights(context.getResources().getColor(R.color.primaryColor), 1000, 5000);


        Intent targetIntent = new Intent(context, TalkActivity.class);
        targetIntent.setData(MultimaniaContract.TalkEntry.buildItemUri(talk.id));
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        NotificationManager nManager = (NotificationManager) context.getSystemService(sContext.NOTIFICATION_SERVICE);
        nManager.notify(NOTIFICATION_ID, builder.build());
    }

    /**
     * Gets the default notification sound
     * @return A Uri indicating the default notification sound
     */
    private static Uri getNotificationSoundUri(){
        return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }

}




Java Source Code List

be.ana.nmct.multimania.ApiTest.java
be.ana.nmct.multimania.ApplicationTest.java
be.ana.nmct.multimania.CalendarTest.java
be.ana.nmct.multimania.DbTest.java
be.ana.nmct.multimania.Import.java
be.ana.nmct.multimania.NotificationImport.java
be.ana.nmct.multimania.ProviderTest.java
be.ana.nmct.multimania.SyncTest.java
be.ana.nmct.multimania.data.ApiActions.java
be.ana.nmct.multimania.data.DbHelper.java
be.ana.nmct.multimania.data.GsonLoader.java
be.ana.nmct.multimania.data.MultimaniaContract.java
be.ana.nmct.multimania.data.MultimaniaProvider.java
be.ana.nmct.multimania.data.NewsItemLoader.java
be.ana.nmct.multimania.data.RoomLoader.java
be.ana.nmct.multimania.data.TagLoader.java
be.ana.nmct.multimania.data.TalkLoader.java
be.ana.nmct.multimania.model.IData.java
be.ana.nmct.multimania.model.NewsItem.java
be.ana.nmct.multimania.model.Room.java
be.ana.nmct.multimania.model.Speaker.java
be.ana.nmct.multimania.model.Tag.java
be.ana.nmct.multimania.model.TalkSpeaker.java
be.ana.nmct.multimania.model.TalkTag.java
be.ana.nmct.multimania.model.Talk.java
be.ana.nmct.multimania.model.User.java
be.ana.nmct.multimania.service.AuthenticatorService.java
be.ana.nmct.multimania.service.Authenticator.java
be.ana.nmct.multimania.service.BootListener.java
be.ana.nmct.multimania.service.NotificationReceiver.java
be.ana.nmct.multimania.service.NotificationSender.java
be.ana.nmct.multimania.service.SyncAdapter.java
be.ana.nmct.multimania.service.SyncService.java
be.ana.nmct.multimania.ui.AboutFragment.java
be.ana.nmct.multimania.ui.LoadActivity.java
be.ana.nmct.multimania.ui.MainActivity.java
be.ana.nmct.multimania.ui.MapFragment.java
be.ana.nmct.multimania.ui.MyScheduleFragment.java
be.ana.nmct.multimania.ui.MySchedulesFragment.java
be.ana.nmct.multimania.ui.NavigationDrawerFragment.java
be.ana.nmct.multimania.ui.NewsFragment.java
be.ana.nmct.multimania.ui.NewsItemActivity.java
be.ana.nmct.multimania.ui.NewsItemFragment.java
be.ana.nmct.multimania.ui.ScheduleFragment.java
be.ana.nmct.multimania.ui.SchedulesFragment.java
be.ana.nmct.multimania.ui.SettingsFragment.java
be.ana.nmct.multimania.ui.SuggestionFragment.java
be.ana.nmct.multimania.ui.TalkActivity.java
be.ana.nmct.multimania.ui.TalkFragment.java
be.ana.nmct.multimania.utils.GoogleCalUtil.java
be.ana.nmct.multimania.utils.SettingsHelper.java
be.ana.nmct.multimania.utils.SettingsUtil.java
be.ana.nmct.multimania.utils.SyncUtils.java
be.ana.nmct.multimania.utils.Utility.java
be.ana.nmct.multimania.vm.NavigationItem.java
be.ana.nmct.multimania.vm.ScheduleTalkVm.java
be.ana.nmct.multimania.widget.StaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.BulletStaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.HeaderFooterListAdapter.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompatIcs.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompat.java