EDIT: The problem below disappeared with no changes in code. The notifications started appearing properly upon later reboots. This is at most an intermittent issue.
I have an application that starts up a background service when the phone boots. After a certain event is detected, I would like to send a notification to the user, allowing the user to elect to start my MainActivity if the user taps on the notification.
The problem is that Android seems to disallow sending notifications if no Activity has yet been launched. The code below is in a custom android.app.Application class, and works only if the Activity has already been launched.
Is there any way to send a Notification before an Activity has been launched?
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Event Detected")
.setContentText("Tap to launch MainActivity")
.setSmallIcon(R.drawable.ic_launcher);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
Here is the error:
05-20 09:09:42.084: W/ContextImpl(801): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1505 com.android.server.StatusBarManagerService.sendNotification:981 com.android.server.StatusBarManagerService.addNotification:670 com.android.server.NotificationManagerService$7.run:2142 android.os.Handler.handleCallback:733
Related
I am searching for a pattern for handling fcm notifications while the app is running, in background or killed.
Our app features a login process and therefor when the app is not launched, the notification needs to open the launcher, but when the app is running i want to just push an activity when the user clicks the notification.
So the best way i guess would be an ContentIntent that gets either delivered to the top most Activity in the Stack or delivered to the Launch-Activity.
My current code always relaunches the app with a TaskStackBuilder no matter if the app is running or not.
A sample of my code:
resultIntent = new Intent(this, MyProfileActivity.class);
resultIntent.putExtra(ActivityUtil.KEY_IMAGE_URL, SharedPref.getUserImage(this));
resultIntent.putExtra(KEY_NOTIFICATION_ID, notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SetDetailReworked.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "...")
.setLargeIcon(getBitmapFromUrl(images[UI.GetDensityInt(this)]))
.setSmallIcon(R.drawable.bar_icon)
.setContentTitle(getString(R.string.push_notification_Title))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(resultPendingIntent);
Notification not = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID, not);
Try to set your MyProfileActivity's launchMode as singleInstance in androidManifest,
<activity
android:name=".MyProfileActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
and it will not relaunches your background activity when user clicks the notification. Instead, MyProfileActivity's onNewIntent will be fired.
I have a list of coordinates (30+ pairs of longitudes-latitudes) and my task is the following:
I should create an android application which sends a notification (on the statusbar) to the user if he/she is on one of the 30 places that my list contains. The notification part is ready, i can do this in an activity (iterating through the list, if coordinates equal, then send the notification, but how can I do this for all activities, and for those times when the app is in cache, but not open? Thanks
Intent intent = new Intent(AGPS.this, SecondClass.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(AnotherGPS.this);
stackBuilder.addParentStack(SecondClass.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent
.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(AnotherGPS.this)
.setSmallIcon(R.drawable.cam_icon)
.setContentTitle("My notification")
.setContentText("content of notification")
.setContentIntent(pendingIntent);
NotificationManager NM = (NotificationManager) getSystemService(Context
.NOTIFICATION_SERVICE);
NM.notify(0, mBuilder.build());
If you are building below api26 you can use background service to listen for location changes, provided your application has permission and the location of mobile is on
Starting api 26 services will not work that way and you most likely get some exceptions, for doing background tasks you have to go through this https://developer.android.com/about/versions/oreo/android-8.0-changes.html#abll
you need to do this in a Service and not in the Activity
take look at this
https://stackoverflow.com/a/46025303/4705092
in onLocationChanged compare locations and if it fits you call notification part
I have created a library, which can send push notifications to an android application. When i put all the code of library to the application, it works but when I try to run them separately, the application doesn't receive any notifications.
The implementation of this function:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(content);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
String bootrapClassName = SharedPreferenceManager.getInstance(this).getClassNameClient();
Intent intent = new Intent(this, Class.forName(bootrapClassName));
stackBuilder.addParentStack(Class.forName(bootrapClassName));
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(DcarBeaconApplication.NOTIFICATION_ID, builder.build());
There is no error or exception, it works fine but doesn't receive any notifications. Please suggest me how to resolve this problem? Thanks.
After try and test some cases, finally, i have to create a service of the library to send the broadcast to the application, and in the application, i register a receiver to listener the service, and to push notification directly from the application.
I'd like an activity of my app to listen when a specific notification is received and do something then.
The notification is triggered from within the app itself. I'm kind of new to Android development (and to development in general) so here's some pseudocode for what I want to do:
Activity{
notificationListener(){
if(notification is received){
//Do something
}
}
Any help would be much appreciated.
After checking the API of Notification, there seems no way to listen to sending Notification. After thinking deep, you would understand why we can't. AFAYK, it is we that write codes to send Notification but not the system, right? We surely know when we send Notifications, right? So, if you want to know when Notification is sent, why not do something, like sending BroadcastReceiver or starting Service to let other codes noticed? It all depends on our code-writers. And here is how to customize a Notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I have a service in background which gives a notification.
I want the next thing:
If I click the notification and the app is not opened open the XActivity.
If the app is opened and the XActivity is created, go there and don't recreate the activity (because if this happen, on back key i will see the same activity again).
My notification code
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New posts!")
.setContentText("New funny posts had just arrived! Click here to see them!");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, XActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
I start the service from the XActivity. (just a activity name example)
Thank you.
i had the same problem, what i did was , i added this line android:launchMode="singleTop" to my activity in manifest, and it worked.
Problem for duplicate activity in your case is because of standard structure design of Activity in Android. For creating only single instance of activity (no duplication) you need to give launchMode property to your activity in your manifest file. You can use singleTop property for this behaviour. You can read more in provided link.
In Addition, for new data access you can use onNewIntent() for new intent coming to this activity and set intent using setIntent() and continue working with new data.
Hope this helps.
Thanks.