-1

Trying to convert my iOS app to android, I know I can't port it so I wrote it from scratch How can I covert this notification to Android Java code?

-(IBAction)turnon {

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:28];
    [comps setMonth:9];
    [comps setYear:2012];
    [comps setHour:8];
    [comps setMinute:25];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *fireDate = [gregorian dateFromComponents:comps];

    UILocalNotification *alarm = [[UILocalNotification alloc] init];

    alarm.fireDate = fireDate;
    alarm.repeatInterval = NSDayCalendarUnit;
    alarm.soundName = @"msl.aiff";
    alarm.alertBody = @"This is a message..";
    alarm.timeZone = [NSTimeZone defaultTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

I've searched the web for like 4 hours now and I think this is simple for an Android developer but since I just started I just don't have any idea how to do this.

Any help is really appreciated!

iTimOSX
  • 31
  • 6
  • I strongly suggest you use the developers site, it has tons of information! http://developer.android.com/guide/topics/ui/notifiers/notifications.html – Rolf ツ Oct 17 '12 at 14:10
  • I've read that but I'm unsure how the Android system works I tried to make an
    OnClick
    function so it shows another window and adding a notification code below that but how to repeat it and will it work?
    – iTimOSX Oct 17 '12 at 14:16
  • Do you want a notification or more something like an notification that shows up at specific times? – Rolf ツ Oct 17 '12 at 14:20
  • A notification at specific times, like above this code shows a notification at 8.25AM every day once the button has been switched on in iOS. – iTimOSX Oct 17 '12 at 14:21

2 Answers2

1

This is what your looking for: You can use the alarm manager to show notifications at specific times, even when your app is not running at all.

http://developer.android.com/reference/android/app/AlarmManager.html

Everyday notifications at certain time

This one is useful to:

Using Alarmmanager to start a service at specific time

Edit see comments:

You can the AlarmManager for this, first create you self some kind of reciever.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent dailyUpdater = new Intent(context, MyService.class);
        context.startService(dailyUpdater);
        Log.d("AlarmReceiver", "started service");
    }
}

Than you need to create the service which is going to show the notifications

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private NotificationManager mNM;


    private int NOTIFICATION = 546;

    public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showNotification();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        mNM.cancel(NOTIFICATION);
        Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();

    private void showNotification() {
        Toast.makeText(this, "show notification", Toast.LENGTH_SHORT).show();

        //notification code here


    }
}

And finally you need to set the alarm:

private void setRecurringAlarm(Context context) {
    Calendar updateTime = Calendar.getInstance();
    updateTime.set(Calendar.HOUR_OF_DAY, 20);
    updateTime.set(Calendar.MINUTE, 30);

    Intent open = new Intent(context, AlarmReceiver.class);
    open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, open, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, 10000, pendingIntent); 
}

And before you make a test run add your Receiver and Service to your manifest file:

<service android:name=".MyService"></service>
<receiver android:name=".AlarmReceiver"></receiver>
Community
  • 1
  • 1
Rolf ツ
  • 8,197
  • 5
  • 44
  • 72
  • I managed to make a notification using this tutorial ([link]http://androidforbeginners.blogspot.nl/2010/02/how-to-create-status-bar-notifications.html[/link]) So it now shows a message when the user hits the ToggleButton, how can I make this to show up at specific times where should I put the code? – iTimOSX Oct 18 '12 at 13:08
  • Thanks @Rolf Smit I'll try this. Can you tell me what downloader.addFlags means/does since this is the only thing that gives me an error. – iTimOSX Oct 18 '12 at 13:26
  • Should be open not downloader ;) changed it – Rolf ツ Oct 18 '12 at 13:32
  • Okay, thanks.. I didn't got it working yet but I'll try to find out what's the problem. – iTimOSX Oct 18 '12 at 13:44
  • Post a new question in that case, and accept questions so other people with the same question will be helped out ;) – Rolf ツ Oct 18 '12 at 13:46
  • Just one last question regarding this before I accept it, The method setRecurringAlarm(Context) from the type MyApp is never used locally they are suggesting to remove it, but then my whole alarm won't work I guess? – iTimOSX Oct 18 '12 at 14:00
  • You should put that method some where in your main activity, and call it for example in the onClick from some kind of button. – Rolf ツ Oct 18 '12 at 14:06
  • You could remove the context argument when you put this method inside a activity, and replace all "context" with "this" because activity is an instance of context. – Rolf ツ Oct 18 '12 at 14:07
  • Still don't know why it doesn't work but will accept it for now, thanks. – iTimOSX Oct 18 '12 at 14:12
  • Looks like I can't post a new question anymore, I added the MyService class and the AlarmManager class into 2 new separate files added the last two lines to de Manifest XML and finally added the "Set Alarm" code to my main App.Java file whenever I run the notification doesn't schedule nor go off. – iTimOSX Oct 18 '12 at 14:21
  • Are you sure you are calling the method inside your main? Did you add the right package declaration in the two java files? – Rolf ツ Oct 18 '12 at 14:24
  • I made some changes in this file http://pastebin.com/qbnrrzb6 please take a look I'm unsure how to explain this otherwise, thanks in advance! – iTimOSX Oct 18 '12 at 14:32
  • do you have an onCreate method in your main? in that case you should add this line: setRecurringAlarm(this); – Rolf ツ Oct 18 '12 at 14:34
  • Thanks a ton Rolf, the notification now shows up but repeats every time but I guess that's just some code fault I made. Thanks again! – iTimOSX Oct 18 '12 at 14:39
  • setRecurringAlarm method has some where a bit of code that controls when the notifications shows up, read the developers docs if you want to know more. – Rolf ツ Oct 18 '12 at 14:55
0

A copy paste with some minor changes from the Android developer site

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
// Sets an ID for the notification, so it can be updated
int mId= 1;
mNotificationManager.notify(mId, mBuilder.build());
Frank
  • 15,379
  • 6
  • 35
  • 48