how to call activity from service? - android

I have created two projects. One project will act as a library and another as a main project. I will call the main project to library project, then the library project will send a notification. It's working fine but my question is if i click the notification it will go to main project activity. What can i do? Is it possible in android?
In library project :
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent("com.sample.myapp.Monitor"), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Start Scheduler")
.setContentText("Please start your activity!")
.setContentIntent(contentIntent).setAutoCancel(true)
.setLights(Color.RED, 1, 1);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
here the com.sample.myapp.Monitor is main project activity.If i click the notification it does not call main project activity.

Finally i got the solution to connect my library project to main project activity.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

I'm not sure if I understood you correctly, so you want to create a notification and when user clicks it you want to show him your Activity, right? In this case you can read Google documentation about that
EDIT: Here you can look a good example of creating a Service with BroadcastReceiver. I assume that your library does some background processing (perhaps in Service as well). So I would suggest the following scenario:
Your application starts a service with registered BroadcastReceiver
Your library project does some background processing and after that sends a broadcast as described in the example provided.
Previosely registered BroadcastReceiver receives a broadcast and sends a notification with action pointing to your Activity (as described in the developer page from my original answer)
User click notification and Activity opens.

Related

click event on firebase notification android

about event for click on notification:
I am searching since yesterday about that , what I found/understood: in FirebaseMessagingService will receive notification data , after will fire local notification, so need to add event in that local notification, I tried to add that many times with many ways but nothing worked …
after I tried to deleted notification files (firebase notification files, and local notification files) but still can receive notification. do you know how to know if the user clicked on the notification ?
To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived and onDeletedMessages callbacks. It should handle any message within 20 seconds of receipt (10 seconds on Android Marshmallow). The time window may be shorter depending on OS delays incurred ahead of calling onMessageReceived. After that time, various OS behaviors such as Android O's background execution limits may interfere with your ability to complete your work.
For further info. you can visit the official website:
Link: https://firebase.google.com/docs/cloud-messaging/android/receive
Hope you'll get your answer here.
Step 1:
// Create an Intent for the activity you want to start
Intent intent = new Intent(this, MainActivity.class);
Step 2:
// Create the PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this, Calendar.getInstance().get(Calendar.MILLISECOND), intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
Step3:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Whenever a user clicks on notification MainActivity will be opened.
Here is details implementation of Android Notification Sample https://github.com/android/user-interface-samples/tree/master/Notifications

clicking on group of notification is not redirecting to specific screen, instead of that app is restarted in oreo

I have implemented firebasse notification functionality in my chat app and everything is running fine but now while testing in Android os 8.0 and 8.1 when app is in background and if user is getting 4 or more than 4 notification then its combined in group and when user click on group then not getting intent and app is restarted.
If user tap on single notification then I am able to send him in specific screen.
I want Notification data or chat id so I can send him in specific screen but not getting any data in intent.
I have searched similar kind of question in stackoverflow but still not getting proper result.
Android: Clicking Grouped Notifications Restarts App
How to open non-launcher activity on notification group click
You can use setContentIntent to set the value of Intent like below
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.your_notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification ")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
It will open SecondActivity (Replace the SecondActivity with your own activity)
I am having the same issue causing app restart in Oreo, after many tries i noticed that if when creating the notification i call .setGroup(string) the system no longer groups the notifications.I am creating the notifications from a service. This is not a solution i know but not grouping is a lesser evil than app restart.

How to add a notification to a MediaPlayer Service?

I have a working MediaPlayer app. The MainActivity (MyListActivity.java) binds itself to a Service (MediaService.java). The Service plays the music in the background.
I would like to know a way to add a notification so the user can control the tracks through it. I am using an Interface to pass the data from the Service to the Activity.
I have no clue where to start. I tried the Notifications tutorial on the Android Documentation, but I don't know how to add buttons and I cannot find a decent tutorial for RemoteViews.
This is what I have for my notification.
MediaService.java
play(Track track) {
...
showNotification(track);
}
showNoticiation(Track track){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.dj_plink_avatar)
.setContentTitle(track.getTitle())
.setContentText(track.getGenre());
Intent resultIntent = new Intent(this, MyListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MyListActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pIntent);
notificationManager = (NotificationManager) getSystemService(Context
.NOTIFICATION_SERVICE);
notificationManager.notify(notificationID, builder.build());
}
PS: Api 15 minimum please :)
NotificationCompat.MediaStyle, part of AppCompat, does all of the styling required and works back to API 7.
You'll still need to build your own PendingIntents for each action, but those can be done any way that makes sense. Generally, that involves using PendingIntent.getService() with an Intent pointing to your Service and an action you can using in onStartCommand() to distinguish which button was pressed.
Note: make sure you are using v7.app.NotificationCompat, not the v4 version.

Android Notification Click - don't backstack activity

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.

How do I make a Statusbar notification that does nothing and goes nowhere?

I have a working statusbar notification for Android already, but to create it I must set an Activity for it to open. I don't want any activity to open; however, this small project doesn't need an interface at all.
mNotificationManager = (NotificationManager) aContext
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
// Create the pending intent, which is basically NOW.
PendingIntent contentIntent = PendingIntent
.getActivity(aContext, 1, new Intent(aContext, BlankActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
// Create notification
notificationBuilder = new NotificationCompat.Builder(aContext)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher) // required for launch
.setTicker("Downloading video...") // required for launch
.setWhen(System.currentTimeMillis()) // should be set for now.
.setContent(remoteView);
BlankActivity is what it sounds like -- an activity that has no content and closes upon being opened. But it still shows up on the list of recently opened windows. I cannot set this to null.
Is there a way I can avoid setting an intent for the status notification at all?
You don't need PendingIntent in that case. Just setting it null in setContentIntent() might help.
Another way can be
PendingIntent contentIntent = PendingIntent.getActivity(aContext(),0,new Intent(),PendingIntent.FLAG_CANCEL_CURRENT);
Try this.
Setting it to null does not work for early versions like SDK 2.3 which requires an intent to be set, otherwise android will choke at receiver's start with the exception:java.lang.RuntimeException: Unable to start receiver com.movistar.android.mimovistar.widgets.WidgetIntentReceiver: java.lang.IllegalArgumentException: contentIntent required
see this post

Categories

Resources