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.
Cause of Notifications Not Showing up on Android Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.
First, turn on notifications & choose your settings Tap Notifications and select a notification level. Tap Inbox notifications. Note: If you're using Android O and above, tap Manage notifications. Under your account, make sure the switch is set to On.
Open your phone's Settings app. Notifications. Under "Lock screen," tap Notifications on lock screen or On lock screen. Choose Show alerting and silent notifications.
You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.
It has to do with NotificationChannel. In Android Oreo and up you can create a NotificationChannel like this:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "my_channel_id";
CharSequence channelName = "My Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{1000, 2000});
notificationManager.createNotificationChannel(notificationChannel);
Then you create your Notification as below:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "my_channel_id";
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("My Message")
.setContentText("My test message!")
.setSmallIcon(R.drawable.ic_notification)
.setChannel(channelId)
.build();
notificationManager.notify(id, notification);
This way The notification will use the proper notification channel and will be displayed correctly. You can also create groups for notification channels. Read more here:
Documenation - NotificationChannel
Examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With