【问题标题】:Notification doesnt work通知不起作用
【发布时间】:2016-05-04 10:10:41
【问题描述】:

我创建了一个通知,但它没有显示出来。 我在主要活动中的代码:`boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);

    if(alarm){
        Intent itAlarm = new Intent("ALARM");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,itAlarm,0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 3);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarme.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, pendingIntent);
    }

我在广播接收器中的代码:

public class BroadcastManager extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    try {
        String yourDate = "04/05/2016";
        String yourHour = "13:07:00";
        Date d = new Date();
        DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
        DateFormat hour = new SimpleDateFormat("HH:mm:ss");
        if (date.equals(yourDate) && hour.equals(yourHour)){
            Intent it =  new Intent(context, MainActivity.class);
            createNotification(context, it, "new mensage", "body!", "this is a mensage");
        }
    }catch (Exception e){
        Log.i("date","error == "+e.getMessage());
    }
}


public void createNotification(Context context, Intent intent, CharSequence ticker, CharSequence title, CharSequence descricao){
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setTicker(ticker);
    builder.setContentTitle(title);
    builder.setContentText(descricao);
    builder.setSmallIcon(R.drawable.web_hi_res_512);
    builder.setContentIntent(p);
    Notification n = builder.build();
    //create the notification
    n.vibrate = new long[]{150, 300, 150, 400};
    n.flags = Notification.FLAG_AUTO_CANCEL;
    nm.notify(R.drawable.web_hi_res_512, n);
    //create a vibration
    try{

        Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone toque = RingtoneManager.getRingtone(context, som);
        toque.play();
    }
    catch(Exception e){}
}

}

【问题讨论】:

  • 原木猫在说什么?

标签: java android notifications push


【解决方案1】:
Above code will start alarm service on every 3 seconds and repeat on every 1 minute. If you want to generate notification on specific time then, You have to add,

calendar.add(Calendar.HOUR, 13);
calendar.add(Calendar.MINUTES, 07);
calendar.add(Calendar.SECONDS, 00);

and remove this line, 

calendar.add(Calendar.SECOND, 3);

I hope it may work for you.

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2018-08-05
    • 2013-05-29
    • 2021-02-09
    相关资源
    最近更新 更多