Reputation: 756
Hello I have a project in uni and I need to add notification to my application. Here's the code for the creation and starting the notification. Everything compile good however it does not display any notifications. Alarm reciever -
package com.example.alex.daily_horoscope;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// fire notification
// Gets an instance of the NotificationManager service
final NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getText(R.string.notification))
.setContentText("You have new horoscope");
// This pending intent will open after notification click
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context, 0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
// Sets an ID for the notification
int mNotificationId = 001;
// Builds the notification and issues it.
mgr.notify(mNotificationId, mBuilder.build());
}
}
Service starter
package com.example.alex.daily_horoscope;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
public class ServiceStarter extends BroadcastReceiver {
public final static int ALARM_ID = 12345;
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Start service");
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
System.out.println("Boot completed");
// start the alarm on phone reboot
Intent intentAlarm = new Intent(context, AlarmReceiver.class);
System.out.println("calling Alarm receiver ");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//set the notification to repeat every fifteen minutes
long startTime = 1*60*1000; // 2 min
// set unique id to the pending item, so we can call it when needed
PendingIntent pi = PendingIntent.getBroadcast(context, ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() +
startTime, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
}
}
}
Upvotes: 0
Views: 3937
Reputation: 1571
in your main activity put this:
Intent intentAlarm = new Intent(context, AlarmReceiver.class);
System.out.println("calling Alarm receiver ");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//set the notification to repeat every fifteen minutes
long startTime = 2*60*1000; // time interval in ms, 2 min in this case
// set unique id to the pending item, so we can call it when needed
PendingIntent pi = PendingIntent.getBroadcast(context, ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() +
startTime, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Upvotes: 1
Reputation: 336
This code will show you a notification in every 1 minute interval.
Replace your AlarmReceiver class with this -
public class AlarmReceiver extends BroadcastReceiver {
int mNotificationId = 001;
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "AlarmReceiver", Toast.LENGTH_LONG).show();
// Gets an instance of the NotificationManager service
final NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(context.getResources().getText(R.string.notification))
.setContentText("You have new horoscope")
.setAutoCancel(true); // clear notification after click
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context, mNotificationId, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mNotificationId, mBuilder.build());
}
}
Add this activity as launcher activity -
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
System.out.println("calling Alarm receiver ");
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
//set the notification to repeat every fifteen minutes
long startTime = 1*60*1000; // 2 min
// set unique id to the pending item, so we can call it when needed
PendingIntent pi = PendingIntent.getBroadcast(this, ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() +
startTime, 60*1000, pi);
}
}
Do not forget to register receiver to your manifest file--
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shajib.customadapter">
<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>
<receiver
android:name=".AlarmReceiver">
</receiver>
</application>
</manifest>
Upvotes: 2