简体   繁体   English

如何通过服务发送通知

[英]How to send Notifications via Service

I am trying to send the notification via service. 我正在尝试通过服务发送通知。 The time of the notification is set from the database table where I have date, time etc columns. 通知的时间是从我有日期,时间等列的数据库表中设置的。 So when System.currentTimeMillis is equal to the time from database column I am trying to send the notification to the user. 因此,当System.currentTimeMillis等于数据库列中的时间时,我尝试将通知发送给用户。 However my implementation doesn't work, could someone please help me? 但是我的实现无法正常工作,有人可以帮我吗? Thanks in advance 提前致谢

Here is my Service file 这是我的服务档案

public class MyService extends Service {

    private NotificationManager notificationManager;
    private DBHelper dbHelper;
    private SQLiteDatabase db;

    @Override
    public void onCreate() {
        Log.i("myLOgs", "Service: onCreate()");
        super.onCreate();
        dbHelper = new DBHelper(this);
        db = dbHelper.getReadableDatabase();
        notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i("myLOgs", "Service: onStartCommand()");

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String currentDateString = dateFormat.format(date);

        SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm");
        Date time = new Date();
        String currentTimeString = timeFormat.format(time);

        String[] columns = {DBHelper.DATE, DBHelper.TIME, DBHelper.EVENT};
        Cursor cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
        cursor.moveToFirst();
        do {
            String dateString = cursor.getString(cursor.getColumnIndex(DBHelper.DATE));
            String timeString  = cursor.getString(cursor.getColumnIndex(DBHelper.TIME));
            String eventString = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT));
            if((currentDateString.equals(dateString)) && (currentTimeString.equals(timeString))) {
                Notification notification = new Notification(R.drawable.ic_launcher, eventString, System.currentTimeMillis());
            }
            if(cursor.moveToLast()) {
                cursor.moveToFirst();
            }
        }while(true);

        //return super.onStartCommand(intent, flags, startId);
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

Simply you can use this method for giving notification from service. 只需使用此方法即可从服务中发出通知。

Just make this method as member of your service class and then you can call this method from service class onStartCommand or wherever you needed. 只要将此方法作为服务类的成员,然后就可以从服务类onStartCommand或任何需要的地方调用此方法。

Fully working i used in my project... 我在我的项目中使用的全部工作...

  private int notificationID = 100;

  private void giveNotification(String notificationTitle, String bodytext)
  {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    @SuppressWarnings("deprecation")
    Notification notification = new Notification(R.drawable.ic_launcher, "New Message", System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, DeliveryReport.class);

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, notificationIntent, 0);

    notification.setLatestEventInfo(getApplicationContext(), notificationTitle, bodytext, pendingIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID++, notification);
  }

To send notification you need to use pending intent 要发送通知,您需要使用待处理的意图

sample code for notification 通知的示例代码

Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Intent notification = new Intent(this, MainActivity.class);
    notification.putExtra("message", message);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0,
            notification, PendingIntent.FLAG_ONE_SHOT);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.logo)
            .setContentTitle("sample").setContentText(message)

            .setAutoCancel(true);

    mBuilder.setContentIntent(pIntent);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(new Random().nextInt(),
            mBuilder.getNotification());

    Ringtone r = RingtoneManager.getRingtone(context, soundUri);

    r.play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM