I have been having difficulties lately getting many specific functions to work on Android Studio. Recently I am working on trying to display simple notifications. They never show up. I feel like I have tried everything at this point. Here's my code.
public class NotificationReceiver extends BroadcastReceiver {
private final String TAG = NotificationReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, ScrollingActivity.class);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addParentStack(ScrollingActivity.class);
taskStackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel");
Notification notification = builder.setContentTitle("My Test Notification")
.setContentText("This is some sample test notification. (Congratulations!)")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}}
Here's the method I use to call the notification.
public void setRepeatingNotification(){
Calendar repeatCalendar = Calendar.getInstance();
repeatCalendar.set(Calendar.HOUR_OF_DAY, 21);
repeatCalendar.set(Calendar.MINUTE, 40);
repeatCalendar.set(Calendar.SECOND, 10);
Intent intent = new Intent(ScrollingActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ScrollingActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, repeatCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(ScrollingActivity.this, "Repeating Notification Set", Toast.LENGTH_LONG).show();
}
Here's the manifest.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@mipmap/skillset_v3"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity
android:name=".ScrollingActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TaskViewActivity"
android:label="@string/title_activity_task_view"
android:parentActivityName=".ScrollingActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
</activity>
<activity
android:name=".MasteryViewActivity"
android:label="@string/title_activity_mastery_view"
android:parentActivityName=".ScrollingActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
</activity>
<service
android:name="TaskTrackingService"/>
<receiver android:name=".NotificationReceiver"/>
</application>
Please help! I have been working on this forever.