I'm writing an Android Application. In this App the user should be able to recieve Notifications in the future. I'm using AlarmManager to send a Broadcast to my Reciever which has an Intent inside with the Notification as Parceled Extra. I don't want to put the whole Code here (its much), so I'm explaining just the workflow and put relevant code here.
I have extended Notification to let itself have an id and an Object from my Model (Model is Parcelable). I take care that the Notification gets Parcelled and unparcelled before my Model.
To send an Alarm I would create a new Notification Instance and call setLatestEventInfo with data from the Model. If I push this Notification directly, everything is fine.
After that, I create a new Pending-Broadcast-Intent. the Intent, which is put inside the Broadcast gets my Notification as ParcelableExtra. Then I fire that PendingIntent Via AlarmManager.set(). if time has come, I recieve the Broadcast in my BroadcastReciever.
I take the extras from my Intent and want to look, id theres a Key "NotificationExtra" which I used to store it. in that call I get an ClassCastException. If you are intereseted in some Code samples, feel free to ask. Any Idea what can go wrong? can I store null Values as Parcelables? And If I can store null values, can they be read properly?
Related
In one of Activity-derived class methods I'm trying to send a Notification, clicking on which will bring the Activity to foreground if my app is in background (not visible) right now. There are reasons why I don't use Service, but use Activity (that will hold PARTIAL_WAKE_LOCK) for that.
I remember Notification worked for me when I used it like this, but I sent it from Service. Now when I send it from Activity method and it doesn't show up, though I hear its notification sound.
So are there any reasons that prevent Notification show up sent from Activity method and how can it be solved?
Thank you.
I was not exactly precise copying Notification code sample. I removed setSmallIcon() call from code as I was too lazy to add icons. As a result system was really dropping my Intent, saying that no icon is provided for it and it'll be a crash in future releases.
We have an app with GCM notifications working fine, we set a PendingIntent so the activity that we want is open when the notification is clicked. However, we need to send an event to Google Analytics each time that one notification (and there are different types of notifications) is clicked. How could we achieve this? I don't want to parse the intent in the activity, as I think that this would not be a good solution (we are using TaskStackBuilder so not always the same activity is open for the same notification), is there a receiver we can use to detect when the notification is clicked/open?
Thanks in advance
put some flag variable on your notification intent.
intent.putExtra("notification","clicked");
Now check it in your Activity, whether Bundle is having "notification" key or not
if(getIntent().hasExtra("notification"))
//write your code for analytics
Try using NotificationListenerService. It allows an application to receive information about notifications. You need to declare the service in your manifest file with the BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action to extend this class. See this example.
I also found this stackoverflow question on how to implement NotificationListenerService using TaskStackBuilder. This might help.
I'm developing a news app, it gets the contents from the news website. I'm making a push notification service by using the new GoogleCloudMessaging technique, so i follow Google's official tutorial and i noticed that they used a PendingIntent, so here are my questions:
What is a pending intent exactly ?
And how does it differ from the normal intent ?
Can i replace it with a normal intent and still can pass my data (extras) to my desired activity ?
Is there any differences in usage and implementation between the two of them ?
Thanks in advance.
A pending intent has a special purpose- it can be passed to other apps to allow them to call into you with the same permissions of your app that other app might not have. PendingIntent should only be used when you're passing it off to another application, such as when setting a Notification (the Notification app can launch your activity via the pending intent). There's no reason to use it internally within your app.
I am sending notifications out from a BroadcastIntentReceiver, however, when I click on the notification, it is from a few broadcasts ago.
I'm using this pattern, however, I do not believe that cancelAll() works. If it did, then wouldn't the fresh Broadcasts be getting their information into these notifs??
mNotificationManager.cancelAll();
mNotificationManager.notify(3, intent);
There is no concept in Android of a Notification being "stale".
I am going to take the educated guess that what you really meant to say was:
I created a Notification with a PendingIntent wrapped around an Intent that contained some extras. Later on, I updated or re-raised that same Notification on a PendingIntent for the same basic Intent but with new extras. Now, however, when I tap on the Notification, I see the original extras, not the current ones.
If so, you need to use FLAG_UPDATE_CURRENT when creating the PendingIntent (e.g., getActivity()).
I am writing an application to listen the SMS inbox in Android with one Activity and one BroadcastReceiver.Once the SMS comes the Receiver is showing Alert message...But i want to send the message information from Receiver to Activity.I don't know how to achieve this.Anybody knows it please help me...
I am writing an application to listen
the SMS inbox in android with one
activity and one BroadcastReceiver.
Please do not do this. This is not part of the Android SDK. Your application will break on some phones. Your application may break in future editions of Android.
But i want to send the message information from Receiver to Activity.
Send another broadcast Intent, this one a private one for use within your own application, where the Activity has registered a BroadcastReceiver (via registerReceiver()) for your private Intent.
You can add 'extras' to the Intent you use to start the Activity using the putExtra methods and retrieve the values using the getExtras method.
This mechanism should be used to pass small (meta-, or parameter-like) data to activities. For bigger data structures it is common to pass an Uri that identifies the data.