how Data transfer can be done from service to activity? - android

I have multiple applications and a single service, i want to create a communication between the apps and the service. I am saving the value in the service which one of the application has sent. Now I am trying to read the same value by some another application from the service.
How this can be achieved? I don't want to call explicit intent from the service and neither implicit intents since implicit intents will give a selection option to choose the desired app, which i don't want. Please advice.

You can use Broadcast Receivers to do this job. You don't need a background service to do this. You can fire an intent and the same can be consumed by other applications which register for the specified intent filter.

using BroadcastReceiver,
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
// Bundle bundle = intent.getExtras();
// String message = bundle.getString("alarm_message");
// Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show();
NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int icon = R.drawable.event;
CharSequence notiText = "Event Notification";
long time = System.currentTimeMillis();
#SuppressWarnings("deprecation")
Notification notification = new Notification(icon, notiText,time);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(context, Setting.class);
//put data in notificationIntent ....>>>>>
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent);
int SERVER_DATA_RECEIVED = 1;
Calendar cal=Calendar.getInstance();
Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show();
if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH))
{
Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show();
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
}
if(intent.getBooleanExtra("Flag",false))
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}

Related

Notification not getting removed on clicking action button even after providing the notification id

I'm delivering a notification which has 2 action buttons namely "Accept" and "Reject".
I'm following this Github repo.
When user clicks "Accept", certain conditions are checked and the logic is performed accordingly.
UPDATE 2.0 - The problem is that upon clicking "Accept" button, operation is happening successfully but the notification isn't disappearing from the status bar because the id generating here: m = (new Random()).nextInt(10000); is different from here: actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); every single time!
Here's the code for notification:
Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationNewRequestService());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);
Here's getNotificationNewRequestService():
private Notification getNotificationNewRequestService() {
mBuilder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle("Title")
.setContentText("text...");
Intent resultIntent = new Intent(getBaseContext(), Profile.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getBaseContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// for action button
Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
actionIntent.putExtra("id", NotificationARBroadcastReceiver.m);
PendingIntent actionPendingIntent = PendingIntent
.getBroadcast(getBaseContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.addAction(R.drawable.ic_accepted_request_black_24dp, "Accept", actionPendingIntent);
mBuilder.addAction(R.drawable.ic_close_black_24dp, "Reject", null);
return mBuilder.build();
}
Here's NotificationARBroadcastReceiver.java file:
public class NotificationARBroadcastReceiver extends BroadcastReceiver {
public static String NOTIFICATION = "notification";
public static NotificationManager mNotifyMgr;
public static int m;
#Override
public void onReceive(Context context, Intent intent) {
m = (new Random()).nextInt(10000);
Log.d("mMain", String.valueOf(m));
mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotifyMgr.notify(m, notification);
}
}
Here's MyBroadcastSender.java file:
public class MyBroadcastSender extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Received by MyBroadcastSender.", Toast.LENGTH_SHORT).show();
int id = intent.getIntExtra("id", 1);
// send back to your class
Intent newIntent = new Intent();
newIntent.setAction(context.getString(R.string.broadcast_id));
newIntent.putExtra("id1", id);
context.sendBroadcast(newIntent);
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();
}
}
and here's MyBroadcastReceiver.java file:
// BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver(){
super();
}
#Override public void onReceive(Context context, Intent intent) {
int id2 = intent.getIntExtra("id1", 1);
if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {
NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);
Intent intent1 = new Intent(MyService.this, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
Toast.makeText(context, "Broadcast received by MyBroadcastReceiver. Now, you can perform actions.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
}
}
}
In getNotificationNewRequestService(), I'm putting notification id as an extra in "id", then in MyBroadcastSender.java, I'm getting this extra as int id = intent.getIntExtra("id", 1); and then putting again as newIntent.putExtra("id1", id); and then finally getting it in MyBroadcastReceiver.java as int id2 = intent.getIntExtra("id1", 1); and trying to remove the notification using it as NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);.
Sorry for this much code, I've to upload it all as they all are necessary.
What I want is to know how to deliver the same notification id from NotificationARBroadcastReceiver.java (which is a separate java file) to MyBroadcastReceiver(which is an inner class in MyService.java)?
Update 1.0- this is what happened when I printed out the values of m, mMain, id, id1:
D/m: 0
D/mMain: 9994
D/id: 0
D/id1: 0
Assuming getNotificationService() == getNotificationNewRequestService() Looks like the NotificationARBroadcastReceiver isn't called before the notfication is built and displayed.
You would do better to generate the notification id where you create the notification and just add it to the intent there as well you don't need to make.
So call getNotificationNewRequestService() from NotificationARBroadcastReceiver.recieve() and make sure the notification ids match up.
Edit:
move:
m = (new Random()).nextInt(10000);
before:
actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); // this will be 'm'
Result:
int m = (new Random()).nextInt(10000);
Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
actionIntent.putExtra("id", m);
Log.d(getClass().getSimpleName(), "Notification Id is : " + m);
then, you can check what values are in id, id1 and id2. Don't forget to call .notify() with same Id you got from m.
You can, also, create getRandomNotificationId() and getLastGeneratedNotificationId() methods. Whenever you generate an Id, store it in public static integer variable, so that you can access it throughout the class.
Problem might be that you are accessing m from NotificationARBroadcastReceiver before initializing it. So, it will definitely be 0. And, you mentioned something about println error, are you using System.out.println()?
Before Edit:
As seen on your new edit, try closing notification before starting it:
m = (...);
// some code here
mNotifyMgr.cancel(m);
mNotifyMgr.notify(m, notification);
and see if your issue gets resolved.

notification can't go current activity

In my notification, I want to start current activity when click in pending intent. In google, many ways found and I tried many times, it didn't work. here is my code,
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String startTime = intent.getStringExtra("Strar_time");
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis(); // notification time
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= notification.FLAG_AUTO_CANCEL;
notification.vibrate = new long[]{100, 200, 100, 500};
notification.defaults = Notification.DEFAULT_ALL;
Intent notificationIntent = new Intent(this, Goo.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent);
notification.contentIntent=contentIntent;
nm.notify(NOTIF_ID, notification);
NOTIF_ID++;
Log.i("NotiID",NOTIF_ID+"");
}
Try this function given below,
private static void generateNotification(Context context, String message) {
int icon = R.drawable.launch_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, Main.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Only one working way I found so far is to make a dummy activity, start it and finish it in onRusume(). However this technique only bring application to front and do not start application when it is closed.
Then I used following workaround to bring application on front if it is alive in background otherwise start it.
Manifest:
<application>
.
.
.
<activity
android:name=".DummyActivity"
/>
.
.
</application>
In code:
public class DummyActivity{
public static boolean isAppAlive;
#Override
protected void onResume() {
// start application if it is not alive
if(!isAppAlive){
PackageManager pmi = getPackageManager();
Intent intent = pmi.getLaunchIntentForPackage(Your applications package name);
if (intent != null){
startActivity(intent);
}
}
finish();
}
In your main activity's onCreateMethod.
#Override
public void onCreate(Bundle savedInstanceState) {
DummyActivity.isAppAlive = true;
}
It is obvious that when your application is fully closed, isAppAlive will restore to its default value which is false. Thus application will be start.

GCM message launching an Intent

Right now I am creating a notification intent as per the following:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MyActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
notificationIntent.putExtra("key", value);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification updateComplete = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msg)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.icon_notifications)
.build();
notificationManager.notify(100, updateComplete);
When the app is already in the background, everything works. The function onNewIntent is called and I get the value from the notificationIntent extras. However, if the app is NOT in the background, it goes to the root activity (login screen) which forwards the user to MyActivity. But the root activity doesn't get the call to onNewIntent, and by the time the user gets to MyActivity, the extras are lost.
Is there anyway around this?
I have been trying to store values elsewhere to no avail... This includes shared preferences.
#Override
protected void onMessage(final Context ctx, Intent intent) {
if (CommonMethod.isAppicationRunning(ctx)) {
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(ctx, ViewMessageDialog.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
resultIntent.putExtra("gcmmessage", message);
ctx.startActivity(resultIntent);
} else {
try {
sendNotification(message);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////////////////
public static boolean isAppicationRunning(Context activity) {
ActivityManager am = (ActivityManager) activity
.getSystemService(EGLifeStyleApplication.mContext.ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.i("current activity", "activity"
+ activity.getClass().getSimpleName());
if (componentInfo.getPackageName().equals(
EGLifeStyleApplication.mContext.getPackageName())) {
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////
private void sendNotification(String message) throws JSONException {
// this
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.app_icon;
CharSequence tickerText = message; // ticker-text
long when = System.currentTimeMillis();
Context context = getApplicationContext();
context.getClass().getSimpleName();
Log.i("context.getClass().getSimpleName()",
"context.getClass().getSimpleName()="
+ context.getClass().getSimpleName());
CharSequence contentTitle = "Product Received.";
CharSequence contentText = message;
Intent notificationIntent = null;
int notificationID = CommonVariable.notificationID;
notificationIntent = new Intent(this, ViewHomeScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
// Play default notification sound
notification.defaults |= Notification.DEFAULT_ALL;
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
mNotificationManager.notify(notificationID, notification);
}
check app running or not?if app is running in forgroung then please call only intent to pass that specific activity.and if app is running in backgroung then call sendnotification() to send notification on notification bar.
//this code is put in your gcmservice
Intent intent2 = new Intent();
intent2.setAction("RECEIVE_MESSAGE_ACTION_NEW");
intent2.putExtra("senderNum", senderNum);
intent2.putExtra("verificationCode",
ReturnValidationcode(message));
context.sendBroadcast(intent2);
Check this out. I don't know why your activity should run to get the upadate. If you follow that tutorial, then the mobile will automatically get push notification, if there are any. No need to run activity on background.
I found out how to do this.
Basically, if the activity you are launching with notificationIntent is already open (if it's the current activity the user is on), it will call onNewIntent(Intent intent). If it is not currently the active activity or if the application is not running, it will launch the activity and go to onCreate().
You have to get the intent from onCreate() by calling getIntent() and checking to see it's not null. Case closed.

Android Status Bar Notifications - Intent getting the old extras on the second time

When I create a notification that is sent through C2DM and received in my app I want to pass some of the data that came with the push notification from C2DM in the intent extras. This works fine the first time I open my notification. Then the data is received with onNewIntent or in onCreate depending on the state of the activity.
But if I send a second push notification through C2DM it is received correctly with the new data but when getting the extras from the intent I still get the data from the previous message. The title and the data I want to see is shown correctly in the notification. So something must be wrong the my intent. This happens if the activity is running and if it isn't.
To create the notification I do the following:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_stat_notify_push, "Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, DesktopApp.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("msg_id", msg_id);
intent.putExtra("title", title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "New message", title + String.valueOf(msg_id), pendingIntent);
notificationManager.notify(0, notification);
To then read the intents:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = getIntent(); // this is just for example purpose
int i = myIntent.getIntExtra("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
#Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if (extras != null) {
int i = extras.getInt("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
}
Any suggestions?
You need to set up a clean flag to PendingIntent:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Have a look at this post that gives a longer explanation.
Use this
PendingIntent pi = PendingIntent.getActivity(context, UNIQUE_ID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

2 notifications, only one I can open, the other did not start my activity

My problem is when I send more than one notification (from the server to the phone via c2dm), I receive all of them with all their information.
I see that I have two icons with notifications. When I click one of them is with my information (I clicked on it, I getActivity), the other is like untrue... didn't start the activity.
I read something about PendingIntent, that start activity once, but I do not know what to do.
I need to get every notification, not only the last (it's not like update).
private void incomingTestp(JSONObject json, Context context) throws JSONException {
Intent intent = new Intent(context, TestpViewActivity.class);
Testp testp = Testp.fromJSON(json);
TestpServiceUtil.save(testp);
intent.putExtra("testpId", testp.getTestpId());
sendNotification("New Testp", "update", intent, testp.getTestpId(), context);
}
private void incomingTestr(JSONObject json, Context context) throws JSONException {
Intent intent = new Intent(context, TestrViewActivity.class);
Testr Testr = Testr.fromJSON(json);
TestrServiceUtil.save(testr);
intent.putExtra("testrId", testr.getTestrId());
sendNotification("New Testr", "update", intent, report.getTestrId(), context);
}
private void sendNotification(String title, String description, Intent intent, long id, Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
intent.putExtra("force", true);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context.getApplicationContext(), title, description, PendingIntent.getActivity(context, requestCode++, intent, Intent.FLAG_ACTIVITY_NEW_TASK));
notification.vibrate = new long[] { 0, 200, 100 };
//notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify("Test", (int) id, notification);
}
I give you a little more information to know how i call this method.
try: PendingIntent.getActivity(context, requestCode++, intent, 0);

Categories

Resources