【问题标题】:Android - Notification at specific time every dayAndroid - 每天特定时间的通知
【发布时间】:2019-01-01 18:40:04
【问题描述】:

我正在尝试向我的应用程序添加通知功能。我希望它每天同时运行通知或操作。我现在有这个通知代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;

public class MainActivity extends AppCompatActivity {

NotificationCompat.Builder notification;
private static final int uniqueID = 45612;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notification = new NotificationCompat.Builder(this);
    notification.setAutoCancel(true);

    // Build the notification
    notification.setSmallIcon(R.drawable.icon);
    notification.setTicker("Brook Betterment Plan");
    notification.setWhen(System.currentTimeMillis());
    notification.setContentTitle("Brook Betterment Plan");
    notification.setContentText("Don't forget to enter your daily stats! ");

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(pendingIntent);

    // Builds notification and issues it
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(uniqueID, notification.build());
}
}

谢谢!希望有人知道答案。另外,我希望它不需要任何额外的 Activity 或 Java 类。

【问题讨论】:

    标签: java android notifications timed


    【解决方案1】:

    报警管理器

    public static void startAlarmBroadcastReceiver(Context context) {
        Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }
    

    报警广播接收器

    AndroidManifest中,定义类为

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

    代码会是这样的

    public class AlarmBroadcastReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        showNotification(context);
    
    }
    
    void showNotification(Context context) {
        String CHANNEL_ID = "your_name";// The id of the channel.
        CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
        NotificationCompat.Builder mBuilder;
        Intent notificationIntent = new Intent(context, TestActivity.class);
        Bundle bundle = new Bundle();
        notificationIntent.putExtras(bundle);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
        if (android.os.Build.VERSION.SDK_INT >= 26) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(mChannel);
            mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLights(Color.RED, 300, 300)
                    .setChannelId(CHANNEL_ID)
                    .setContentTitle("Title");
        } else {
            mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setContentTitle("Title");
        }
    
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setContentText("Your Text");
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(1, mBuilder.build());
    }
    

    }

    【讨论】:

    • 我认为没有必要检查android OS版本。只需将 Channel 用于所有 android 设备。
    • 谢谢!这么多的答案对我不起作用,但这个答案非常清晰,而且很有魅力。太好了!
    【解决方案2】:

    创建一个将生成通知的类,例如 LocalNotification

    在类似 showDailyNotification 的方法下添加您在该类中创建时的代码

    现在使用 JobScheduler 并每 24 小时安排一次作业,当作业开始时在 LocalNotification 中调用此方法

    将 LocalNotification 设为单例类。

    顺便说一句,不要忘记使用通知通道,否则它将无法在奥利奥及以上设备上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      相关资源
      最近更新 更多