使用服务在特定时间发出通知

时间:2015-01-18 21:47:49

标签: java android-service

我正在为穆斯林开发Android祈祷应用程序。我已经开发了代码,以便在praytimes.org的帮助下获得祈祷时间。现在我想做到这一点,以便当时通知用户祈祷。但就目前而言,我只是希望能够在特定时间发送通知。看看代码和在线资源,在我看来好像它应该工作,但由于某种原因,通知没有显示。我知道通知部分的代码是正确的,因为我创建了一个按钮来使用该代码创建通知,并且它可以工作。所以我认为问题出在服务,接收器或alarmManager上。

我的猜测是问题与MyNotificationService.java中的onStartCommand()方法有关。我不太熟悉服务和他们的生命周期,所以请放轻松我。

提前致谢。

在我的MainActivity.java中,这是我的onCreate()方法中的代码。

/*========================Begin Notification Code==============================*/

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 18);

calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);

Intent intent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

/*========================End Notification Code==============================*/

这是MyReceiver.class

package com.ahmed.omar.tawheed;

/**
 * Created by Omar on 1/18/15.
 */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service = new Intent(context, MyNotificationService.class);    
        context.startService(service);

    }
}

这是MyNotificationService.java

package com.ahmed.omar.tawheed;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;


public class MyNotificationService extends Service  {

    private NotificationManager notificationManager;
    private int prayerNotificationID = 420; //blazeIt

    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub  
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent1, int flag, int startId) {

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Notification n  = new NotificationCompat.Builder(this)
                .setContentTitle("Time to Pray")
                .setContentText("It is now time to pray salah")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .build();

        notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        n.flags = Notification.FLAG_AUTO_CANCEL;

        // Sending the notification itself
        notificationManager.notify(prayerNotificationID, n);

        return START_STICKY;
    }


    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

以下是我的AndroidManifest.xml

中的行
<service android:name=".MyNotificationService"
        android:enabled="true" />

    <receiver android:name=".MyReceiver"/>

2 个答案:

答案 0 :(得分:0)

请参阅Calendar.MONTH的文档。有效值从0开始(1月),而不是1.因此,您的代码尝试在2月18日而不是1月18日设置闹钟。尝试将Calendar.MONTH值更改为0而不是1.

答案 1 :(得分:0)

这就是我要发送的通知。

  fun showNotification(context: Context,title: String, message: String) {
    Log.e("notification", "visible")
    val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val channel = NotificationChannel("1",
            "NAMAJ_CHANNEL",
            NotificationManager.IMPORTANCE_DEFAULT)
        channel.description = "namaj alarm channel"
        mNotificationManager.createNotificationChannel(channel)
    }
    val mBuilder = NotificationCompat.Builder(context, "1")
        .setSmallIcon(com.waltonbd.muslimcalender.R.mipmap.ic_launcher) // notification icon
        .setContentTitle(title) // title for notification
        .setContentText(message)// message for notification
        .setAutoCancel(true) // clear notification after click
    val intent = Intent(context, MainActivity::class.java)
    val pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    mBuilder.setContentIntent(pi)
    mNotificationManager.notify(0, mBuilder.build())
}