i m generating notification.so i created the notification and i m getting the notification when i click the button.now,i created onclick function and some toast,but when i clicked on thone button in notificatio i m getting toast message. i dont know whats wrong in below code.so please help.thanks in adavance.
this is my broadcast activity
public class NotificationBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(NotificationGenerator.NOTIFY_PLAY))
{
Toast.makeText(context,"NOTIFY PLAY",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_PAUSE))
{
Toast.makeText(context,"NOTIFY PAUSE",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_NEXT))
{
Toast.makeText(context,"NOTIFY NEXT",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_DELETE))
{
Toast.makeText(context,"NOTIFY DELETE",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals("com.example.murarilal.atry.previous"))
{
Toast.makeText(context,"NOTIFY PREVIOUS",Toast.LENGTH_LONG).show();
}
}
}
this is notifiction builder activity
public class NotificationGenerator{
public static final String NOTIFY_PREVIOUS="com.example.murarilal.atry.previous";
public static final String NOTIFY_DELETE="com.example.murarilal.atry.delete";
public static final String NOTIFY_PAUSE="com.example.murarilal.atry.pause";
public static final String NOTIFY_PLAY="com.example.murarilal.atry.play";
public static final String NOTIFY_NEXT="com.example.murarilal.atry.next";
private static final int NOTIFICATION_ID_OPEN_ACTIVITY=1;
private static String ID = "default";
public static void openActivityNotification(Context context) {
RemoteViews expandView=new RemoteViews(context.getPackageName(),R.layout.notification);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
int requestID = (int) System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setChannelId(ID);
Intent notifyIntent=new Intent(context,BlankFragment2.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,requestID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
Intent intent = new Intent(context, BlankFragment2.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ed)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle("try")
.setCustomBigContentView(expandView)
.setContentText("notification")
.setContentInfo("Info");
notificationManager.notify(NOTIFICATION_ID_OPEN_ACTIVITY, notificationBuilder.build());
}
public static void customBigNotification(Context context)
{
int requestID = (int) System.currentTimeMillis();
RemoteViews expandView=new RemoteViews(context.getPackageName(),R.layout.notification);
NotificationCompat.Builder nc=new NotificationCompat.Builder(context);
NotificationManager nm=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notifyIntent=new Intent(context,BlankFragment2.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,requestID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_play_circle_filled_black_24dp);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandView);
nc.setContentTitle("music player");
nc.setContentText("control audio");
nc.getBigContentView().setTextViewText(R.id.songName,"shape of you");
setListeners(expandView,context);
}
private static void setListeners(RemoteViews views, Context context)
{
int requestID = (int) System.currentTimeMillis();
Intent previous= new Intent(NOTIFY_PREVIOUS);
Intent next=new Intent(NOTIFY_NEXT);
Intent delete=new Intent(NOTIFY_DELETE);
Intent play=new Intent(NOTIFY_PLAY);
Intent pause=new Intent(NOTIFY_PAUSE);
PendingIntent pPrevious=PendingIntent.getBroadcast(context,requestID,previous,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.previous,pPrevious);
PendingIntent pDelete=PendingIntent.getBroadcast(context,requestID,delete,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.cancel,pDelete);
PendingIntent pNext=PendingIntent.getBroadcast(context,requestID,next,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.next,pNext);
PendingIntent pPlay=PendingIntent.getBroadcast(context,requestID,play,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.play,pPlay);
PendingIntent pPause=PendingIntent.getBroadcast(context,requestID,pause,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.play,pPause);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.murarilal.atry">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/mytheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
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=".screen"
android:label="#string/title_activity_screen"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".com.layout.blankFragment2"
android:label="#string/hello_blank_fragment"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.albumSongs"
android:label="#string/title_activity_album_songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name="com.exlayout.Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".albumSongs"
android:label="#string/title_activity_album_songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".albumSong"
android:label="#string/title_activity_album_song"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".BlankFragment2"
android:label="#string/hello_blank_fragment"
android:theme="#style/AppTheme.NoActionBar" />
<receiver android:name=".NotificationBroadcast">
<intent-filter>
<action android:name="com.example.murarilal.atry.previous" />
<action android:name="com.example.murarilal.atry.delete" />
<action android:name="com.example.murarilal.atry.pause" />
<action android:name="com.example.murarilal.atry.next" />
<action android:name="com.example.murarilal.atry.play" />
</intent-filter>
</receiver>
<activity
android:name=".genreSongs"
android:label="#string/title_activity_genre_songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.genreSongs"
android:label="#string/title_activity_genre_songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".artist_Songs"
android:label="#string/title_activity_artist__songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.artist_Songs"
android:label="#string/title_activity_artist__songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".checkActivity"
android:label="#string/title_activity_check"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".favoutitesActivity"
android:label="#string/title_activity_favoutites"
android:theme="#style/AppTheme" />
<activity
android:name=".recent"
android:label="#string/title_activity_recent"></activity>
</application>
</manifest>
Related
I am trying to show notification at specific date I am using alarm manager and broadcast receiver to show notifications but the problem is notification works only when app is open and when app is closed notification does not show. Below is my code:
Reminder.java
public class Reminder extends AppCompatActivity {
long reminderDateTimeInMilliseconds = 000;
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
but = findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotifyChannel();
Intent intent = new Intent(Reminder.this,ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this,0,intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();
//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2020, 8, 20, 19, 12, 0);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
alarmManager.setExact(AlarmManager.RTC_WAKEUP,reminderDateTimeInMilliseconds,pendingIntent);
}
});
}
private void createNotifyChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "ReminChannel";
String desc = "This is my channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("mynotif",name,importance);
channel.setDescription(desc);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
ReminderBroadcast.java
public class ReminderBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder notif = new NotificationCompat.Builder(context,"mynotif")
.setContentTitle("Appointment reminder")
.setContentText("Hello there")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(200,notif.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.firstapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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=".Reminder">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ThirdActivity" />
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
</activity>
<receiver android:name=".ReminderBroadcast"/>
</application>
</manifest>
What am I doing wrong?
I've changed my Launcher Activity instead of .MainActivity. The Firebase push notifications service doesn't work when my app is in the background but it works fine when the app is in the foreground.
I've added a WelcomeSlider in my app that's why I have to keep Welcome slider as the Launcher Activity. I checked that if I change the launcher activity to NotifyActivity then it works fine again.
here is my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".WelcomeActivity"
android:label="#string/app_name"
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=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Vlog"
android:parentActivityName=".MainActivity" />
<activity android:name=".NotifyActivity"></activity>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Here is myFirebaseInstanceIdService:
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh(){
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN",token);
}
}
Here is myFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this,NotifyActivity.class);
if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Bundle bundle = new Bundle();
bundle.putString("url",url);
intent.putExtras(bundle);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.notifyicon);
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(sound);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
}
}
I've solved this problem by my own.I just keep the MainActivity as a launcher Activity from Android Manifest like
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and post below code inside MainActivity onCreate for checking if its the first time launch or not!
//Check if it first time launching..
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}
it worked for me! :) :)
I'm trying to configure an alarm notification in Android and unfortunately no alarm is generated.
Here is the creation of the alarm:
myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
t.setDueDate(myDate);
t.setHasDate(true);
Intent alarmNotificationIntent = new Intent(this, ReminderNotification.class);
alarmNotificationIntent.putExtra("task", t);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, (int) task_id, alarmNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getDueDate().getTime());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
In addition I've created:
NotificationReceiver
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiver extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
ReminderNotification
import android.content.Context;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
public class ReminderNotification extends BroadcastReceiver
{
public ReminderNotification()
{
// TODO Auto-generated constructor stub
}
#Override
public void onReceive(Context context, Intent intent) {
// The PendingIntent to launch our activity if the user selects this notification
Task task = (Task)intent.getSerializableExtra("task");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
Intent snzInt = new Intent(context, SnoozeReminderReceiver.class);
snzInt.putExtra("task", task);
PendingIntent snoozeIntent = PendingIntent.getBroadcast(context, 0,snzInt, PendingIntent.FLAG_CANCEL_CURRENT);
Intent doneInt = new Intent(context,DoneActionReceiver.class);
doneInt.putExtra("task", task);
PendingIntent doneIntent = PendingIntent.getBroadcast(context, 0,doneInt, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
The manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.she.dd.todoli"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<receiver android:name="ReminderNotification"/>
<receiver android:name="SnoozeReminderReceiver"/>
<receiver android:name="DoneActionReceiver"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ListNodeActivity"
android:label="#string/title_activity_list_node"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="il.ac.shenkar.david.todolistex2.MainActivity" />
</activity>
<activity
android:name=".Signup_Activity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity
android:name=".create_team"
android:label="Create new task team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".invite_member"
android:label="Invite Members Your Team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Login_activity"
android:label="Login to Wiggle"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
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=".EditTaskActivity"
android:label="Edit Task"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
I have a feeling that I'm missing something very small but cannot find the issue.
Please let me know what I'm doing wrong.
Thanks in advance.
you have to first build notification and then notify it...
NotificationManager _manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("New")
.setContentText("hello")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pintent)
.build();
notification.vibrate=new long[]{100,250,100,500};
_manager.notify(notification_id, notification);
You can find a complete example of Alarm and Notification in my this GitHub Repo
I have the AlarmMainActivity which broadcasts an intent at a time set by the alarm. And the receiver program AlarmReceiver should catch this intent and send a notification. From the log, I can see that the alarm is being set, but the receiver doesn't start and its not working. Can you look at my code below and please let me know why the BroadcastReceiver is not working. Thanks.
AlarmMainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_main);
Intent alertIntent = new Intent(this, AlarmReceiver.class);
final PendingIntent pendingIntent =
PendingIntent.getBroadcast
(this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT);
final Button alarmButton = (Button)findViewById(R.id.alarm_button);
alarmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Long alertTime = new
GregorianCalendar().getTimeInMillis()+5*1000;
Log.i(TAG,"Alarm will be sent at : "+ alertTime.toString());
AlarmManager am =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,alertTime,pendingIntent );
Log.i(TAG, "Alarm is now set");
}
});
}//oncreate
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver{
private static final String TAG = "AlarmReceiver";
private String msgTitle, msgText, msgTicker;
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Entered AlarmReceiver()");
msgTitle = "Todays Weather";
msgText = "Its Sunny and Warm";
msgTicker="Alert";
PendingIntent notifyIntent = PendingIntent.getActivity(context,0, new
Intent(context, AlarmMainActivity.class),0);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(context).
setSmallIcon(R.drawable.weather_image).
setTicker(msgTicker).
setContentTitle(msgTitle).
setContentText(msgText).
setDefaults(NotificationCompat.DEFAULT_SOUND).
setAutoCancel(true).
setContentIntent(notifyIntent);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService
(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,mBuilder.build());
Log.i(TAG,"Notification Sent");
}
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thirdlaw.alertalarm" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true">
<activity
android:name=".AlarmMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
<intent-filter>
<action android:name="MY_ACTION_STRING"/>
</intent-filter>
</receiver>
</activity>
</application>
</manifest>
Remove the <receiver /> tag out of the <activity /> tag like :
<activity
android:name=".AlarmMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
<intent-filter>
<action android:name="MY_ACTION_STRING"/>
</intent-filter>
</receiver>
I am trying to use alarm manager to show notification after 5 sec. I have tried many sites but was not able to understand please give a simple example for explaining how to use alarm manager and connect notification with it.
I am newbie.
this is function i used to set alarm and I am not getting notification after 5 secs actually not at all not in emulator nor in android mobile. If put code to create notififcation with a button pressing that is working great.
public void setAlarm(View view)
{
Intent alertIntent = new Intent(this, AlertReciver.class);
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
tv.setText("completed");
}
and this class to make it work
public class AlertReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"times Up", "5 SEcond has passed", "Alert");
}
public void createNotification(Context context,String msg, String msgText, String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ne)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepaksingh.goinnotifying" >
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
You havn't set permission for wake:
<uses-permission android:name="android.permission.WAKE_LOCK" />
ANd you didn't register your recevier.Here.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<receiver android:name=".AlertReciver "/>
</application>