I have to develop an one push notification application.
i did like :
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
regId = GCMRegistrar.getRegistrationId(this);
String message = intent.getExtras().getString("offer");
displayMessage(context, message);
// notifies user
if(Constants.response != null){
generateNotification(context,message);
}
}
private static void generateNotification(Context context,String message) {
int icon = R.drawable.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 = new Intent(context, MyDealProducts.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
here the notification is generated successfully on logged in customer.
Now didn't visit these notification at the same time logged out the customer form which means i don't need to enable the notification.
But here the notification is enabled.How can i disable the notification after logged out the customer ???
please give me a suggestions ???
For this you can cancel the Notification that you have generated, whenever the user is logged out.
Use the method cancel(int id) (Where id is the notification id, in your case, its 0) or cancelAll() of NotificationManager.
Related
I have a task in which I have to show Notification for an incoming message inside an activity using BroadcastReceiver with the address and content on the SMS. Now I have to click in order to insert the values in sms in database. When the task is finished i want it to clear from the activity. Is it possible to create notification for incoming sms inside an Activity?
You can Try This no generate your own Notification.
#SuppressWarnings("deprecation")
private static void generateNotification(Context context) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "New Contact Added",
when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent intent = PendingIntent.getActivity(context, iUniqueId,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, "New", 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(2, notification);
}
please help me out. I am stuck with GCM push message.
Everything working perfect but when I am trying to display my message on next screen I am getting always old one or first one.
But If I check my log cat, I am getting new message always from the server. So where is problem I am not getting. I have tried many code on stackoverflow. Here is the snippet of my code-
// this is the my service class
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(SENDER_ID);
}
// this is the onMessage revive method
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "new message= ");
String message = intent.getExtras().getString("message");
generateNotification(context, message);
System.out.println(message+"++++++++++1");
}
// this is the notification method
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_launcher;
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);
String subTitle = "Important News for you!";
Intent notificationIntent = new Intent(context, NotificationView.class);
notificationIntent.putExtra("content", message);
//PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent);
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
public class NotificationView extends Activity {
// and this is the my next activity where i am displaying push message-
Intent intent=getIntent();
stringValue=intent.getStringExtra("content");
System.out.println(stringValue);
I have already try-
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
and this-
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Both did not work for me.
Code is fine ,Try using String Buffer may because String Data cant be change unless new is specified every time im new here give me vote up :)
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.
I've an app that i can push notifications on to via GCM. I receive the notification in the notification drawer but when i click on it, an activity from the app is started(EntryActivity). What i'd like to happen is if the message in the notification is a url to the .apk file eg 'http://path_to_file.apk', then i'd like the file to start downloading.
Below is the code that generates the notification, is there something i need to add or change to start the download?
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
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 = new Intent(context, EntryActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
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);
}
Try this:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://path_to_file.apk"));
notificationIntent.setClassName("com.android.browser",
"com.android.browser.BrowserActivity");
I have successfully set up the code to run GCM over phonegap on an android app. I have managed to secure the handset registration ID and able to send a message to the app using this ID in a PHP script.
My only problem is that the message displays as a javascript alert whilst the app is open, and I am looking to have message sent to the handset's but not showing icon on top notifications bar .
**> does is possible showing icon on top bar in android device
Does anyone know if the GCM plugin for Phonegap is capable of doing this?**
I have using C2DM-Phonegap.
https://github.com/marknutter/GCM-Cordova
please help me friends...
The library you are using specifies what needs to be done when a message is received. You can either modify their code to do what you want or roll your own GCMIntentService.
please refer this links :
https://github.com/phonegap-build/PushPlugin
https://build.phonegap.com/plugins/324
The library you are using specifies what needs to be done when a message is received. You can either modify their code to do what you want or roll your own GCMIntentService.
The quickest way is to add the following code to GCMIntentService.java into the onMessage function. The GCM plugin you are using only receives the GCM message, but you have to trigger notifications on your own. You can also try a cordova plugin for status bar notifications.
protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "onMessage - context: " + context);
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
String message = extras.getString("message");
String title = extras.getString("title");
Notification notif = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis() );
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
Intent notificationIntent = new Intent(context, SomeActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notif.setLatestEventInfo(context, "Title of notification", message, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
mNotificationManager.notify(1, notif);
please check this code
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
// here is your icon drawable instead of ic_launcher
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 = new Intent(context.getApplicationContext(), devicephp.class);
// set intent so it does not start a new activity
notificationIntent.putExtra("msg", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}