我正在尝试使用Calendar和AlarmManager在特定时间发送推送通知,但是一旦到达指定时间,就不会发送任何通知。
我看过其他有关如何执行此操作的教程,我已经直接关注了它们,但似乎都没有用。
我也试图将我的NotificationReceiver类包含在AndroidManifest.xml文件中,但是我收到一条错误消息,指出该类不可分配给android.app.Activity。
但是,我无需使用AlarmManager就可以使它正常工作。
RepeatingActivity是在单击通知后重定向用户的位置,它是一个琐碎的活动,上面带有一个简单的文本框。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNotification(View view) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 14);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(getApplicationContext(),
NotificationReceiver.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_DAY, pendingIntent);
}
NotificationReceiver.java
public class NotificationReceiver extends BroadcastReceiver {
// Called when class gets triggered
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeatIntent = new Intent(context, RepeatingActivity.class);
repeatIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
100, repeatIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new
NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Notification Title")
.setContentText("Notification Text")
.setAutoCancel(true);
// Create channel for notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("channelID",
"name", importance);
notificationManager.createNotificationChannel(channel);
builder.setChannelId("channelID");
}
notificationManager.notify(100, builder.build());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jessicathornsby.myapplication.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="Do it!" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.family.lagoc.myapplication">
<uses-permission android:name="android.permission.SET_ALARM"></uses-permission>
<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>
<activity android:name=".RepeatingActivity"></activity>
</application>
</manifest>
当我加入这一行时:
<activity android:name=".NotificationReceiver"></activity>
,
我收到此错误:
com.family.lagoc.myapplication.NotificationReceiver is not assignable to android.app.Activity
用户单击activity_main.xml文件上的按钮,然后在指定的时间设置警报(我将其硬编码在其中,仅用于测试目的)。但是,我希望那时能收到通知,但什么也没收到。