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.
Related
Unfortunately another question about my startForegroundService notification... I searched, really, I did:
I have a foreground service that is running perfectly. I would like to add a couple of actions to this notification. For one, make it so when the user clicks the notification they are sent to MainActivity as well as adding a "Quit" addAction.
Here is the snippet I am using to create the notification:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getNotificationChannel(notificationManager);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My app")
.setContentText("Background service is running...")
.setContentIntent(pendingIntent)
.build();
startForeground(13365, notification);
Using the above a notification shows up just fine, but click on it results in nothing. I also tried using addAction, also nothing. I am aware the syntax is a little bit different (....Action.Builder) when adding an addAction.
I am creating my notification in the onCreate handler of the foreground service. Running on SDK 26.
Can startForeground notifications have setContentIntent / addAction attached to them?
Thanks!
Solved : I had the notification replaced elsewhere in my application and was not adding the intents there.
Doh!
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 am creating an android application that shows a notification in a Service.
I show the notification exactly as mentioned in documentation. It works fine and the PendingItent object starts the desired activity.
But after a while when I close and restart the application a couple of times, tapping on the notification will start the Main Activity (which is the parent of desired activity) instead of the desired activity.
The only way I found to fix that is to restart the device. (Clearing app's cache and uninstalling didn't help.
I googled my problem and also searched here a lot, but couldn't find anything relevant.
seems that I'm the only one having this problem.
Could anyone please help me with this problem?
Thanks in advance;
EDIT:
as I said, I do exactly like the documentation. But here is my code anyway:
Intent resultIntent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MyActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Body");
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(457, builder.build());
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