简体   繁体   English

间隔发送一次android通知

[英]Send a notification with android at an interval

I'm trying to set a notification to appear after a certain interval. 我正在尝试将通知设置为在一定间隔后出现。 I expected this to work however nothing seems to happen when running the code on my phone other than at reciever being printed to the console and I can't seem to find out why. 我以为这可以工作,但是在手机上运行代码时似乎什么也没发生,除了在接收方打印到控制台时,我似乎找不到原因。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is the code used to set the alarm. 这是用于设置警报的代码。

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 10);
Intent intent = new Intent(getApplicationContext(), NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIn

Here is the notification receiver class 这是通知接收器类

public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("at reciever");
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeatingIntent = new Intent(context, LoginActivity.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("Please Leave at the cage")
            .setContentText("It almost 5")
            .setAutoCancel(true);

    notificationManager.notify(100, builder.build());
}
}

Here are the relevant manifest lines 这是相关的清单行
receiver android:name=".NotificationReciever" uses-permission android:name="com.android.alarm.permission.SET_ALARM" 接收者android:name =“。NotificationReciever”使用权限android:name =“ com.android.alarm.permission.SET_ALARM”

Basically you need to set a icon for each notifications. 基本上,您需要为每个通知设置一个图标。

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("Please Leave at the cage")
            .setSmallIcon(R.drawable.crosshair); //crosshair is the icon choice in this example
            .setContentText("It almost 5")
            .setAutoCancel(true);

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

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