Oreo 在一小时内安排通知

Posted

技术标签:

【中文标题】Oreo 在一小时内安排通知【英文标题】:Oreo Scheduling a Notification in an hour 【发布时间】:2018-12-21 23:52:36 【问题描述】:

android Oreo 中,我尝试将通知安排在已知的未来时间。我写了一小段代码来测试我是否可以在应用程序可能关闭的一个小时内安排通知。当我尝试这样做时,什么也没有发生。非常感谢您的帮助。

public class MainActivity extends AppCompatActivity 
private NotificationManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NotificationChannel notificationChannel = new NotificationChannel("default",
            "primary", NotificationManager.IMPORTANCE_DEFAULT);
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(notificationChannel);
    Intent notifyIntent = new Intent(this, MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    long milisInFuture = 1000 * 60 * 60;
    alarmManager.set(AlarmManager.RTC_WAKEUP, milisInFuture, pendingIntent);


public class MyNewIntentService extends JobIntentService 
    @Override
    protected void onHandleWork(@NonNull Intent intent) 
        Notification notification = new Notification.Builder(getApplicationContext(), "default")
                .setContentTitle("title")
                .setContentText("body")
                .setSmallIcon(android.R.drawable.stat_notify_chat)
                .setAutoCancel(true)
                .build();
        manager.notify(123, notification);
    


public class MyReceiver extends BroadcastReceiver 
    public MyReceiver() 
    

    @Override
    public void onReceive(Context context, Intent intent) 
        Intent intent1 = new Intent(context, MyNewIntentService.class);
        context.startService(intent1);
    


如果有帮助,这里是清单。

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name=".MainActivity$MyNewIntentService"
        android:permission="android.permission.BIND_JOB_SERVICE"></service>
</application>
</manifest>

【问题讨论】:

你卡在哪里了? 我认为您缺少所需的通知 Chanel @Brandon,您尚未在清单文件中添加您的接收器。 【参考方案1】:

更新:

不要忘记在清单 xml 文件中添加接收器。 对于这个问题:

在应用标签内添加AndroidManifest.xml:

&lt;receiver android:name=".MyReceiver" /&gt;

示例: 我保留这个例子,它会更新来自广播接收器类的通知。

如图所示创建一个广播接收器类,(不要忘记在清单xml文件中添加接收器)

public class NotificationUpdate extends BroadcastReceiver 
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) 
        //NEED A RESCHEDULE?
        updateNotification(context);
    

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void updateNotification(Context context)
        Notification notification = new Notification.Builder(context.getApplicationContext(), "default")
                .setContentTitle("title")
                .setContentText("body")
                .setSmallIcon(android.R.drawable.stat_notify_chat)
                .setAutoCancel(true)
                .build();
        NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        if(manager!=null) manager.notify(123, notification);
    

Activity 调用示例:

public class MainActivity extends AppCompatActivity 

    final long intervalPeriod=60*1000;
    AlarmManager mAlarmManager;

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

        NotificationChannel notificationChannel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            notificationChannel = new NotificationChannel("default",
                    "primary", NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) manager.createNotificationChannel(notificationChannel);

            mAlarmManager=(AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
            PendingIntent intent=PendingIntent.getBroadcast(getApplicationContext(),1234,
                    new Intent(getApplicationContext(),NotificationUpdate.class),0);

            mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+intervalPeriod, intent);

        
    

【讨论】:

以上是关于Oreo 在一小时内安排通知的主要内容,如果未能解决你的问题,请参考以下文章

安排每小时重复本地通知的问题

如何安排本地通知

每天在准确的时间安排通知

在一天中的特定时间安排本地通知

如何在接下来的 12 小时内每小时安排 Jenkins 作业

如何使用 Quartz 安排作业在一天内多次但固定的时间运行