I create 2 app (appA, appB) , from appA i send message to appB, in appB a catch this message and create notification , but i want on i click notification open my appA and see my message.
appA
public class MainActivity extends Activity {
TextView text;
Button send;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.editText);
send = (Button) findViewById(R.id.button);
View.OnClickListener Onlistbtn = new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(view.getContext(),Main2Activity.class);
intent.putExtra("com.example.abc.send_messege.broadcast.Message",text.getText().toString());
intent.setAction("com.example.abc.send_messege.custom_action");
sendBroadcast(intent);
}
};
send.setOnClickListener(Onlistbtn);
}}
appB
public class MyReceiver extends BroadcastReceiver {
private final static AtomicInteger c = new AtomicInteger(3);
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String text = intent.getStringExtra("com.example.abc.send_messege.broadcast.Message");
//Intent intent1 = new Intent(context, Main2Activity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.alert_dark_frame)
.setContentTitle("My notification")
.setContentText(text)
.setContentIntent(pIntent)
.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(getID(), mBuilder.build());
}
public static int getID() {
return c.incrementAndGet();
}}
AndroidManifest appB
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.abc.send_messege.custom_action" />
</intent-filter>
</receiver>
Ok you can try this
We you are creating the PendingIntent pass the new intent to launch activity from another application(appA)
Intent intent = new Intent();
//com.colisa.broadcast is the package of another app
//com.colisa.broadcast.MainActivity is the actity to be launched
intent.setComponent(
new ComponentName("com.colisa.broadcast","com.colisa.broadcast.MainActivity"));
Then you will pass it to the PengingIntent.setContentIntent(intent)
I haven't triend this but i think you can intent.putExtra(key, message) and on receiving Activity get that messsage via (this post)
if (null != getIntent().getExtras().getString(key)){
// whatever
}
Related
Actually I'm implementing firebasePushNotification service which is working wihtout any issue . Now when I'm receiving push_notification and my app is in foreground by clicking the notification intent is being passed to the desired activity without any error. But when the app is in background or not opened clicking on the notification is not sending the intent to the desired activity and even it's not showing any error . I tried to refer some questions in SO about adding exported=true option in manifest.xml but it couldn't solve my issue.
Code of NotificationManager Class:
public void displayNotification(String title, String body,String post_owner_username,String post_owner_time,
String notif_commenter_username,String notif_comment_time,String follower_username,String type) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx, Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.new_icon)
.setContentTitle(title)
.setContentText(body);
Intent i = new Intent(ctx, ProfileHolder.class);
if (type.equals("likes")) {
Bundle b = new Bundle();
b.putString("post_owner_username", post_owner_username);
b.putString("post_owner_time", post_owner_time);
b.putString("what", "show_notification_post");
i.putExtras(b);
i.putExtra("Open", "starred");
}
else if (type.equals("comments")) {
Bundle b = new Bundle();
b.putString("post_owner_username",post_owner_username);
b.putString("post_owner_time", post_owner_time);
b.putString("notif_commenter_username", notif_commenter_username);
b.putString("notif_comment_time",notif_comment_time);
b.putString("what", "show_notification_post");
i.putExtras(b);
i.putExtra("Open", "starred");
}
else if (type.equals("comment_likes")) {
Bundle b = new Bundle();
b.putString("post_owner_username", post_owner_username);
b.putString("post_owner_time", post_owner_time);
b.putString("notif_commenter_username", notif_commenter_username);
b.putString("notif_comment_time", notif_comment_time);
b.putString("what", "show_notification_post");
i.putExtras(b);
i.putExtra("Open", "starred");
}
else if (type.equals("follow")||type.equals("follow_request")) {
Bundle b = new Bundle();
b.putString("searchUsername", follower_username);
i.putExtras(b);
i.putExtra("Open", "search_profile");
}
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_ONE_SHOT);
/*
* Setting the pending intent to notification builder
* */
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mBuilder.setAutoCancel(true);
mNotifyMgr.notify(1, mBuilder.build());
}
}
If you are using firebase push notification
You need to send a click_action attribute ...to open the desired activity when app is in background.
So try ..
Intent i = new Intent(click_action);
click_action is the intent-filter mapping to the desired activity. click_action is mandatory
instead of the Explicit intent call - explicit intent call in the above case will only work when the app is in foreground..
Here is how I've managed to open specific activity from notification when app is in the background/closed and to pass data through Intent.
Application
public class AppConfig extends Application {
#Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
Manifest
<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.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- Don't forget to set exported attribute to true -->
<activity android:name=".MainActivity" android:exported="true"/>
MessagingService
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String body = null;
if(remoteMessage.getNotification() != null) {
body = remoteMessage.getNotification().getBody();
}
String id = null;
// get data that server passed (if it passed any at all)
if(remoteMessage.getData().size() > 0) {
id = remoteMessage.getData().get("id");
}
sendNotification(body, id);
}
private void sendNotification(String messageBody, String id) {
// put data you want to send through intent
Bundle bundle = new Bundle();
bundle.putString("id", id);
// create intent, put data, and
Intent intent = new Intent(this, ActivityLogin.class);
intent.putExtras(bundle);
// IMPORTANT! clearing previous tasks so desired activity will launch
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// customising the notification
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
When app is closed and a notification comes, user clicks it and a specific user's Profile opens up.
It has a parent activity (UsersActivity) but in this case when back button in the toolbar is clicked, the app is closed.
Back button is working as expected when user goes to profile activity from UsersActivity.
I want it to go parent activity even app starts with a notification.
How can I do that?
here is my manifest:
<activity
android:name=".UsersActivity"
android:parentActivityName=".MainActivity"
>
</activity>
<activity
android:name=".ProfileActivity"
android:parentActivityName=".UsersActivity"
>
This is ProfileActivity:
public class ProfileActivity extends AppCompatActivity {
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mToolbar = findViewById(R.id.profile_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_gold_back);
getSupportActionBar().setLogo(R.drawable.ic_logo_v);
getSupportActionBar().setTitle(R.string.app_name);
getSupportActionBar().setSubtitle(R.string.profile_toolbar_title);
Notification's class:
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getData().get("title");
String notification_message = remoteMessage.getData().get("body");
String click_action = remoteMessage.getData().get("click_action");
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}
}
maybe your onBackPressed() method in your Activity is doing a finish(); of the current Activity and there is no other Activities in the background, so it will close the app.
To solve this you can always return to your MainActivity with an intent from your UserActivity
if the user opens the notifications and goes to their profiles you can implement a method in your ProfileActivity.class with this
#Override
public void onBackPressed() {
startActivity(new Intent(this,MainActivity.class));
}
Also in your code, make sure you are not doing a finish(); in your activity when you are closing it, because it will disapear from the current stack and then if you enter again to your app, like i said, you will be closing the app since there is no Activities in the backgroud.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode!=KeyEvent.KEYCODE_BACK)return false;
//enter the required code here to go to the necessary activity
return true;
}
Hi i am making a habit tracker app and when a new habit is created by user i call sendNotification() method for calling notifications at time specified by user.I want to show these notifications everyday at time specified by user.
Now notifications are showing up when app is running or when app is minimized but when i close app (not from settings) notifications are shown.
Here's my code:
private void sendNotification(){
NotificationReceiver.setupAlarm(this, notificationCalendar);
}
public class NotificationReceiver extends WakefulBroadcastReceiver {
public NotificationReceiver() {
}
public static void setupAlarm(Context context, Calendar notificationCalendar) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationCalendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = NotificationIntentService.createIntentStartNotificationService(context);
startWakefulService(context, serviceIntent);
}
private static PendingIntent getStartPendingIntent(Context context) {
Intent intent = new Intent(context, NotificationReceiver.class);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
public class NotificationIntentService extends IntentService {
private static final int NOTIFICATION_ID = 1;
public NotificationIntentService() {
super(NotificationIntentService.class.getSimpleName());
}
public static Intent createIntentStartNotificationService(Context context) {
return new Intent(context, NotificationIntentService.class);
}
#Override
protected void onHandleIntent(Intent intent) {
try{
processStartNotification();
}finally {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
private void processStartNotification() {
// Do something. For example, fetch fresh data from backend to create a rich notification?
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Habit Time")
.setContentText("Hey time for your habit")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSound(alarmSound)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
}
}
//Manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".notification.NotificationReceiver"/>
<service
android:name=".notification.NotificationIntentService"
android:enabled="true"
android:exported="false"/>
The notification won't show until you make a background service for them. Create a background service and send broadcast from that service, this service will keep running weather your app is running or not. You can check this detailed answer.
Make a NotificationService class and extend it like below.
public class NotificationService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#SuppressWarnings("deprecation")
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
/* send your notification from here, in timely manner */
}
}
Start is from your activity like this
Intent i = new Intent("com.example.package.NotificationService");
startService(i);
This code is written to give you an idea. I didn't test it.
Try the following code: it creates an alarm for a specific time in a day using alarmanager and repeat it daily...
Declare
Calendar cal_alarm;
Now set the calender object for specific time to alarm
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MM yyyy hh:mm a");
String time=""21 12 2016 8:10 AM"
cal_alarm = Calendar.getInstance();
cal_alarm.setTime(dateFormat.parse(time));
Set the alarm
public void set_alarm() {
Calendar calNow = Calendar.getInstance();
long current_time = calNow.getTimeInMillis();
Log.d("ALARM CALENDER VALUES", cal_alarm.toString());
long alarm_time_in_millis = cal_alarm.getTimeInMillis();
//check if time is alreday passed or not
if (alarm_time_in_millis > current_time) {
new Alarm_task(getApplicationContext(), cal_alarm).run();
}
public class Alarm_task implements Runnable {
// The date selected for the alarm
private final Calendar cal;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;
long alarm_time2;
int _id;
public Alarm_task(Context context, Calendar cal) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.cal = cal;
this._id = (int) System.currentTimeMillis();
alarm_time2 = cal.getTimeInMillis();
//Toast.makeText(getActivity(), alarm_time2 + " ", Toast.LENGTH_SHORT).show();
}
#Override
public void run() {
// Request to start are service when the alarm date is upon us
// We don't start an activity as we just want to pop up a notification into the system bar not a full activity
Intent i = new Intent("com.package_name");
i.setAction("com.package_name");
/** Creating a Pending Intent */
PendingIntent operation = PendingIntent.getActivity(getApplicationContext(), _id, i, PendingIntent.FLAG_UPDATE_CURRENT);
/** Converting the date and time in to milliseconds elapsed since epoch */
long alarm_time = cal.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time each day*/
am.setRepeating(AlarmManager.RTC_WAKEUP, alarm_time, AlarmManager.INTERVAL_DAY, operation);
}
}
Now in manifest define an explicit activity which handle the alarm intent and show popup dialog and notification during alarm time
<activity
android:name=".Prereminder"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
//same as your alarm task
<action android:name="com.package_name" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Now in your Prereminder.java
public class Prereminder extends FragmentActivity {
String msg = "Helloo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
sendNotification();
/** Creating an Alert Dialog Window */
Reminder_alert alert = new Reminder_alert();
/** Opening the Alert Dialog Window */
alert.show(getSupportFragmentManager(), "Reminder_alert");
}
private void sendNotification() {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
//handle notification on click
Intent myintent = new Intent(this, Home_page.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_small)
.setContentTitle("Alarm")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
Now your Reminder_alert class show the popup dialog during alarm:
public class Reminder_alert extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
/** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
/** Creating a alert dialog builder */
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/** Setting title for the alert dialog */
builder.setTitle("ALARM ON");
/** Setting the content for the alert dialog */
builder.setMessage("WAKE UP NOW");
/** Defining an OK button event listener */
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
/** Creating the alert dialog window */
return builder.create();
}
/**
* The application should be exit, if the user presses the back button
*/
#Override
public void onDestroy() {
super.onDestroy();
getActivity().finish();
}
}
1- in simple way first use this function to create alarm
private void createAlarm(Date start_alarm_date, String schedual_type ,String schedule_id){
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, PushLocalNotification.AlarmReceiver.class);
intent.setAction("Your_Action_Name"); //this action you will use later
intent.putExtra("Extra", any_extra_you_want_add);// remove if you want add extra
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// Set the alarm to start at specific date
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// repeat it "daily":
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
2- create IntentService and make your receiver extend BroadcastReceiver
public class PushLocalNotification extends IntentService {
private NotificationManager mNotificationManager;
private String mScheduleID;
NotificationCompat.Builder builder;
public PushLocalNotification() {
super("pushLocalNotification");
}
#Override
protected void onHandleIntent(Intent intent) {
// call create local notification here
}
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("Your_action_Name")) {
// call service here.
Intent sendIntent = new Intent(context, PushLocalNotification.class);
sendIntent.putExtra("Extra_name", intent.getStringExtra("Previos_extra_you_add_before"));
context.startService(sendIntent);
}
}
}
private void createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//Create Intent to launch this Activity again if the notification is clicked.
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intent);
// Sets the ticker text
builder.setTicker(getResources().getString(R.string.custom_notification));
// Sets the small icon for the ticker
builder.setSmallIcon(R.drawable.ic_stat_custom);
// Cancel the notification when clicked
builder.setAutoCancel(true);
// Build the notification
Notification notification = builder.build();
// Inflate the notification layout as RemoteViews
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
// Set text on a TextView in the RemoteViews programmatically.
final String time = DateFormat.getTimeInstance().format(new Date()).toString();
final String text = getResources().getString(R.string.collapsed, time);
contentView.setTextViewText(R.id.textView, text);
/* Workaround: Need to set the content view here directly on the notification.
* NotificationCompatBuilder contains a bug that prevents this from working on platform
* versions HoneyComb.
* See https://code.google.com/p/android/issues/detail?id=30495
*/
notification.contentView = contentView;
// Add a big content view to the notification if supported.
// Support for expanded notifications was added in API level 16.
// (The normal contentView is shown when the notification is collapsed, when expanded the
// big content view set here is displayed.)
if (Build.VERSION.SDK_INT >= 16) {
// Inflate and set the layout for the expanded notification view
RemoteViews expandedView =
new RemoteViews(getPackageName(), R.layout.notification_expanded);
notification.bigContentView = expandedView;
}
// START_INCLUDE(notify)
// Use the NotificationManager to show the notification
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, notification);
}
}
3- final step don't forget to declare service and receiver inside manifest file
<receiver
android:name=".PushLocalNotification$AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="Your_Action_name:)" />
</intent-filter>
</receiver>
<service android:name=".PushLocalNotification" />
be patient and have fun :)
Create a method which contains your Code where you will define your Time or at what time you want to show the notification.This method need to be called from where you want user to ask for notification.
public void getNotification () {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 00);
if (now.after(calendar)) {
Log.d("Hey","Added a day");
calendar.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
Create a Notification_receiver class which is going to extend Broadcast Receiver here you are going to define your Channel Id as it is perfectly working for API 25 and above this the Notification_receiver class:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;
//Created By Prabhat Dwivedi
public class Notification_receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("Your App Name",
"You app Package Name",
NotificationManager.IMPORTANCE_HIGH);
String channel_Id = channel.getId();
CharSequence channel_name = channel.getName();
Log.e("Notification_receiver", "channel_Id :" + channel_Id);
Log.e("channel_name", "channel_name :" + channel_name);
channel.setDescription("Make entry of today's spending now");
notificationManager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.yourapp_logo)
.setChannelId("Your app Name is your Channel Id")
.setContentTitle("Your title")
.setContentText("Your Description")
.setAutoCancel(true);
//nder this you will find intent it is going to define after clicking notification which activity you want to redirect
Intent repeatingIntent = new Intent(context, HomePage.class);
pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
notificationManager.notify(100, builder.build());
}
}
Kindly also add the Notification receiver inside AndroidManifest.xml file
<receiver android:name=".Notification_receiver"/>
I'm working on app which test state on server every 15 min and push notification , i used Alarm Manager , broadcast receiver & Intent Service .
every thing worked fine and i get this state from server perfectly when app is running or in background , until i removed it from recent apps, every thing stops and can't get that state from server.
I searched ... and get nothing , but my friend tell me that I must register my broadcast receiver in on create of class extend from application.
I don't know how to do this .. so I need help please
Main Activity Class
public class MainActivity extends AppCompatActivity {
static TextView TvText;
Button Btn11, Btn22;
AlarmManager alarm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
TvText = (TextView) findViewById(R.id.tv_Text);
Btn11 = (Button) findViewById(R.id.btn_11);
Btn22 = (Button) findViewById(R.id.btn_22);
Btn22.setEnabled(false);
}
public void Btn11OC(View view) {
scheduleAlarm();
Btn11.setEnabled(false);
Btn22.setEnabled(true);
}
public void Btn22OC(View view) {
if (alarm!= null) {
cancelAlarm();
}
Btn11.setEnabled(true);
Btn22.setEnabled(false);
}
// Setup a recurring alarm every half hour
public void scheduleAlarm() {
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), broadtest.class);
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, broadtest.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
900000L, pIntent);
}
public void cancelAlarm() {
Intent intent = new Intent(getApplicationContext(), broadtest.class);
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, broadtest.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.cancel(pIntent);
}
}
Broad Cast Receiver
public class broadtest extends WakefulBroadcastReceiver {
public static final int REQUEST_CODE = 12345;
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyService.class);
context.startService(i);
}
}
AppController Class
public class AppController extends Application {
#Override
public void onCreate() {
super.onCreate();
}
}
MyService class
public class MyService extends IntentService {
static int NOTIFICATION_ID = 0;
public MyService() {
super("MyService");
}
#Override
protected void onHandleIntent(Intent intent) {
String url = "http://test.com/testts.php";
// Tag used to cancel the request
String tag_string_req = "string_req";
StringRequest strReq = new StringRequest(Request.Method.GET,
url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Volley Log", response);
Toast.makeText(MyService.this, response, Toast.LENGTH_SHORT).show();
if (response.equals("0")){
sendNotification("Titel Test 1111", "Body Test 1111");
}else if (response.equals("1")){
sendNotification("Titel Test 2222", "Body Test 2222");
}else {
sendNotification("Titel Test 3333", "Body Test 3333");
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MyService.this, error.toString(), Toast.LENGTH_SHORT).show();
VolleyLog.d("Volley Log", "Error: " + error.getMessage());
}
});
// Adding request to request queue
int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
strReq.setRetryPolicy(policy);
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
// Setup periodic alarm every 5 seconds
}
private void sendNotification(String title, String messageBody) {
long[] pattern = {500,500,500,500,500,500,500,500,500};
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(alarmSound)
.setLights(Color.BLUE, 500, 500)
.setVibrate(pattern);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (NOTIFICATION_ID > 1073741824) {
NOTIFICATION_ID = 0;
}
notificationManager.notify(NOTIFICATION_ID++, notificationBuilder.build());
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gih.testmass">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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=".broadtest"
android:process=":remote">
</receiver>
<service
android:name=".MyService"
android:exported="false">
</service>
</application>
You need to start the service as a foreground service. When you clear app from recents it kills the service
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.mipmap.ic_launcher))
.setContentTitle("WhatsApp Reminder Service.")
.setContentText("Touch to configure.");
Intent startIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 965778, startIntent, 0);
builder.setContentIntent(pendingIntent);
startForeground(965778, builder.build());
return START_REDELIVER_INTENT;
}
It is necessary to build a notification when you use foreground service.
Hope it helps.
I see you have used IntentService
see answer to this question
Using startForeground() with an Intent Service
To run a process on a device when users are not particularly interacting with your application.
The steps will involve:
1.Updating your android manifest xml
2.Setting up broadcast receivers to listen to relevant events
3.Set up a background service for context when your application isn’t running
Android menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission ... />
<application
android:name=".MyApplication"
... >
<receiver android:name=".receivers.PeriodicTaskReceiver">
<intent-filter>
<action android:name="com.example.app.PERIODIC_TASK_HEART_BEAT" />
</intent-filter>
</receiver>
<service android:name=".services.BackgroundService" />
...
</application>
Now for the Broadcast receiver
public class PeriodicTaskReceiver extends BroadcastReceiver {
private static final String TAG = "PeriodicTaskReceiver";
private static final String INTENT_ACTION = "com.example.app.PERIODIC_TASK_HEART_BEAT";
#Override
public void onReceive(Context context, Intent intent) {
if (!Strings.isNullOrEmpty(intent.getAction())) {
MyApplication myApplication = (MyApplication) context.getApplicationContext();
SharedPreferences sharedPreferences = myApplication.getSharedPreferences();
if (intent.getAction().equals("android.intent.action.BATTERY_LOW")) {
sharedPreferences.edit().putBoolean(Constants.BACKGROUND_SERVICE_BATTERY_CONTROL, false).apply();
stopPeriodicTaskHeartBeat(context);
} else if (intent.getAction().equals("android.intent.action.BATTERY_OKAY")) {
sharedPreferences.edit().putBoolean(Constants.BACKGROUND_SERVICE_BATTERY_CONTROL, true).apply();
restartPeriodicTaskHeartBeat(context, myApplication);
} else if (intent.getAction().equals(INTENT_ACTION)) {
doPeriodicTask(context, myApplication);
}
}
}
private void doPeriodicTask(Context context, MyApplication myApplication) {
// Periodic task(s) go here ...
}
public void restartPeriodicTaskHeartBeat(Context context, MyApplication myApplication) {
SharedPreferences sharedPreferences = myApplication.getSharedPreferences();
boolean isBatteryOk = sharedPreferences.getBoolean(Constants.BACKGROUND_SERVICE_BATTERY_CONTROL, true);
Intent alarmIntent = new Intent(context, PeriodicTaskReceiver.class);
boolean isAlarmUp = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE) != null;
if (isBatteryOk && !isAlarmUp) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmIntent.setAction(INTENT_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
}
public void stopPeriodicTaskHeartBeat(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, PeriodicTaskReceiver.class);
alarmIntent.setAction(INTENT_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
alarmManager.cancel(pendingIntent);
}
}
here com.example.app.PERIODIC_TASK_HEART_BEAT is application’s own broadcast, created and sent from our restartPeriodicTaskHeartBeat method.
your Alarmmanager should have this line
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
Now for your background service class:
public class BackgroundService extends Service {
private static final String TAG = "BackgroundService";
PeriodicTaskReceiver mPeriodicTaskReceiver = new PeriodicTaskReceiver();
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyApplication myApplication = (MyApplication) getApplicationContext();
SharedPreferences sharedPreferences = myApplication.getSharedPreferences();
IntentFilter batteryStatusIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatusIntent = registerReceiver(null, batteryStatusIntentFilter);
if (batteryStatusIntent != null) {
int level = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPercentage = level / (float) scale;
float lowBatteryPercentageLevel = 0.14f;
try {
int lowBatteryLevel = Resources.getSystem().getInteger(Resources.getSystem().getIdentifier("config_lowBatteryWarningLevel", "integer", "android"));
lowBatteryPercentageLevel = lowBatteryLevel / (float) scale;
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Missing low battery threshold resource");
}
sharedPreferences.edit().putBoolean(Constants.BACKGROUND_SERVICE_BATTERY_CONTROL, batteryPercentage >= lowBatteryPercentageLevel).apply();
} else {
sharedPreferences.edit().putBoolean(Constants.BACKGROUND_SERVICE_BATTERY_CONTROL, true).apply();
}
mPeriodicTaskReceiver.restartPeriodicTaskHeartBeat(BackgroundService.this);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
startSelf();
}
}
Here the Backgroundservice tries to find the device’s low battery threshold and set our battery control flag appropriately before it attempts to restart the Broadcast receiver.
And START_STICKY will try to re-create your service after it is killed and call onStartCommand() again with a null intent.
Finally for your Application class start the Background service:
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
#Override
public void onCreate() {
super.onCreate();
// Initialize the singletons so their instances
// are bound to the application process.
...
Intent startServiceIntent = new Intent(context, BackgroundService.class);
startService(startServiceIntent);
}
}
For detail implementation see this:https://technology.jana.com/2014/10/28/periodic-background-tasks-in-android/
be sure to make you service like this in your Mainifest
<service
android:name=".service.youservice"
android:exported="true"
android:process=":ServiceProcess" />
then your service will run on other process named ServiceProcess
if you want make your service never die :
onStartCommand() return START_STICKY
onDestroy() -> call startself
if nothing works use startForeground() service..
I broadcast an Intent : com.ss.CUSTOM_INTENT
/*-----Class BroadcastReceiverApp-------*/<br/>
public class BroadcastReceiverApp extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.broadcast_activity);
}
/* #Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}*/
// broadcast a custom intent.
public void broadcastIntent(View view){
Intent intent = new Intent();
intent.setAction("com.ss.CUSTOM_INTENT");
sendBroadcast(intent);
}
}
/*------------------------*/
But when I receive that broadcast intent, I want it to be shown in notification drawer.
The Receiver class can extend only BroadcastReceiver class, hence it is now allowing to extend Activity class there by I cannot use:
Context context = getApplicationContext();
which is to be further used in Notification.Builder(context)....
This is my the Broadcast Receiver class:
public class MyReceiver extends BroadcastReceiver , Activity{
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";
Context cont = getApplicationContext();
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent = PendingIntent.getActivity(
MyReceiver.this,
0,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification = new Notification.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("http://android-er.blogspot.com/")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
Log.i(getClass().getSimpleName(),"Sucessfully Changed Time");
}
}
Can anyone help in this ? I want to later change the broadcast intent to "android.intent.action.DATE_CHANGED/TIME_CHANGED" which is even not working.
But when I receive that broadcast intent, I want it to be
shown in notification drawer.
If your question is "how to update the gui from a BroadcastReceiver" you have to put the BroadcastReceiver class inside the Activity that you want to update.
The nested BroadcastReceiver class can access the elements of the outer Activity.
For an example see
Android BroadcastReceiver within Activity