I am developing an android application in titanium, in which I included google cloud messaging. I am able to get registration id from server. Also when my application is running I am able to receive the message from my server. But When my application is running in background At that time my application is able to receive message.But when I clicked on notification it does not resume my application. I added the following code in my app.js:
var intent = Titanium.Android.createIntent({
action: Titanium.Android.ACTION_MAIN,
className: 'com.nrcomps.rtlireportsandroid.RtlIreportsAndroidActivity',
packageName: 'com.nrcomps.rtlireportsandroid',
flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP |
Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK
});
intent.addCategory(Titanium.Android.CATEGORY_LAUNCHER);
var pending = Ti.Android.createPendingIntent({
activity : Ti.Android.currentActivity,
intent : intent,
type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
flags : Ti.Android.FLAG_ACTIVITY_NEW_TASK
});
Any help would be appreciated.
Thanks
You need to set the Intent flags on the Intent. You were specifying them in the call to get a PendingIntent.
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
String st = "message";
NotificationManager notifymanManager = (NotificationManager) getSystemService(ns);
int s = R.drawable.icon;
long when = System.currentTimeMillis();
Notification nt = new Notification(s, st, when);
Intent intent1 = new Intent(this, Woobme.class);
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
intent1.setAction(Intent.ACTION_MAIN);
nt.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent pd = PendingIntent.getActivity(this, 0, intent1, 0);
nt.setLatestEventInfo(context, st, st, pd);
int i = 1;
int hello_id = i;
long m[] = { 0, 100, 200, 200 };
nt.defaults |= Notification.DEFAULT_SOUND;
nt.vibrate = m;
notifymanManager.notify(hello_id, nt);
Related
I've got a problem with my Android Application. It receives Messages via Google Cloud Messaging and displays a Notification in the Notificationbar. I want to transfer some Data with the Notification. This Data comes via Google Cloud Messaging and should be attached to the Notification.
This is my Code to Display the Notification:
private void setNotification(String strTitle, String strMessage, int intPageId, int intCategorieId)
{
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentOpen = new Intent(this, NotificationClick.class);
intentOpen.putExtra("pageId", Integer.toString(intPageId));
intentOpen.putExtra("categorieId", Integer.toString(intCategorieId));
intentOpen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentOpen, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.launcher_icon)
.setTicker(strTitle)
.setContentTitle(strTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(strMessage))
.setContentText(strMessage);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify(intCategorieId, notificationBuilder.build());
}
And my Code to get the Variables "intPageId" and "intCategorieId" in a other Activity(witch opens by clicking the Notification) back, is this:
Bundle intentExtras = new Bundle();
intentExtras = getIntent().getExtras();
Toast.makeText(this, "Page To Open: " + intentExtras.getString("pageId"), Toast.LENGTH_LONG).show();
Toast.makeText(this, "Page To Open: " + intentExtras.getString("kategorieId"), Toast.LENGTH_LONG).show();
But in both "Toast" Messages appears "null", or the value that was passed through at the first time, clicking on the Message.
For Example:
At the First time I send: intPageId=1.
The Toast says: "Page To Open: 1".
At the Second time I send: intPageId=2.
The Toast says: "Page To Open: 1".
I'm totally confused.
First Method :
Instead of below line :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentOpen, 0);
change it to this :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentOpen, PendingIntent.FLAG_UPDATE_CURRENT);
Second Method :
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),iUniqueId, intentForNotification, 0);
I'm implementing a notification service and I have problems to receive the messages in Android 2.3. I receive the messages in versions 4.0 and newer but not in 2.3. In logcat appears the following errors:
Could not find class 'com.google.android.gms.ads.internal.b.c', referenced from method com.google.android.gms.ads.internal.n.e.a
.
.
Could not find class 'android.app.Notification$Builder', referenced from method com.google.android.gms.common.l.a
What could be the problem? This is my method to send the notification:
private void sendNotification(String msg) {
MainActivity.notificationClicked = true;
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
String strMessage = loadPreferences();
String newMessage = "";
if (!strMessage.isEmpty())
newMessage = strMessage + "<br>" + msg;
else
newMessage = msg;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.text, Html.fromHtml(newMessage));
Notification notification = new Notification(icon, Html.fromHtml(msg), when);
notification.contentView = contentView;
String title = this.getString(R.string.app_name);
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClass().getSimpleName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
Intent notificationIntent;
if(!componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){
notificationIntent = new Intent(getApplicationContext(),
FirstActivity.class);
} else {
notificationIntent = new Intent(getApplicationContext(),
MainActivity.class);
notificationIntent.putExtra("login", true);
}
notificationIntent.putExtra("message", Html.fromHtml(msg));
oldMessage = newMessage;
savePreferences(getApplicationContext(), oldMessage);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
int notifyID = 1;
PendingIntent intent = PendingIntent.getActivity(this, notifyID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.contentIntent = intent;
notificationManager.notify(notifyID, notification);
}
When I send the message I receive it in device with Android 4.2 for example but not in devices with Android 2.3. I'm debugging the app with Android 2.3 and I have put a breakpoint in the method onHandleIntent of the IntentService but it never comes. I think that it could be for the errors that appears in logcat.
How can I solve the problem for devices with Android 2.3?
Thanks in advance.
I change Notification to NotificationCompat:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.contentView = contentView;
notificationManager.notify(notifyID, notification);
but the problem persist, in logcat appears the same error
Try this code. I've used this in my project and it runs good. receiverActivity is activity that will open onclick, as I can remember, but I'm not 100% sure of it.
public static void showNotification ( Context context, Class receiverActivityClass, String title, String text, int icon )
{
Intent intent = new Intent( context, receiverActivityClass );
PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, 0 );
// Build notification
Notification notification = new Notification.Builder( context )
.setContentTitle( title )
.setContentText( text )
.setSmallIcon( icon )
.setContentIntent( pendingIntent )
.setAutoCancel( true )
.getNotification();
NotificationManager notificationManager =
( NotificationManager ) context.getSystemService( context.NOTIFICATION_SERVICE );
notificationManager.notify( 0, notification );
}
I'm donloading files from the Internet using IntentService and displaying a Notification while download is in progress. After download complete I need to be able to click on the notification to open downloaded file in appropriate application. Here's a code I'm using for this:
Intent intent = IntentUtils.getOpenFileIntent(task.getTargetFolder()
+ File.separator + task.getFileNode().getName());
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);
PendingIntent pi = taskStackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
And here's how I create the Intent:
public static Intent getOpenFileIntent(String path) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = fileExt(path);
String type = mime.getMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, type);
return intent;
}
The issue is that tapping on the notification closes any of the currently opened app. I just need to display application chooser over the currently opened app. I think that the issue is in TaskStackBuilder usage but there's no other way to create PendingIntent instance for ACTION_VIEW Intent.
You need to create PendingIntent and set to your Notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_CANCEL_CURRENT)
Fore more information go to http://developer.android.com/guide/topics/ui/notifiers/notifications.html
private NotificationManager manager;
private Intent notiIntent;
/// id is integre value which in unique for notifications
notiIntent= new Intent(context, CurrentActiivty.class);
notiIntent.putExtra("id", id);
notiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification notification = new Notification(R.drawable.icon, "your text on notification bar", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notiIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context,"text here", message, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notiIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
manager.notify(id, notification);
Now In calling actiivty write the following code
Bundle extras = getIntent().getExtras();
int id= extras.getInt("id");
NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(id);
I am trying to open the browser with a url when the user click on the push notification, i search in stackoverflow and i find this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
but it doesnt work for me, when i put that the notification doesnt appear, i debugged it and it only throw the class file editor no error or anything.
this is the code
public void mostrarNotificacion(Context context, String body,String title, String icon, String url,String superior)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notManager = (NotificationManager) context.getSystemService(ns);
int icono = R.drawable.mydrawable;
CharSequence textoEstado = superior;
long hora = System.currentTimeMillis();
Notification notif = new Notification(icono, textoEstado, hora);
Context contexto = context.getApplicationContext();
CharSequence titulo = title;
CharSequence descripcion = body;
PendingIntent contIntent;
if(url.equalsIgnoreCase("NULL"))
{
Intent notIntent = new Intent(contexto,MainActivity.class);
contIntent = PendingIntent.getActivity(
contexto, 0, notIntent, 0);
}
else
{
// Intent i = new Intent(Intent.ACTION_VIEW);
//i.setData(Uri.parse(url));
// contIntent = PendingIntent.getActivity(contexto, 0, i, 0);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
// notif.setLatestEventInfo(contexto, titulo, descripcion, contIntent);
//AutoCancel:
notif.flags |= Notification.FLAG_AUTO_CANCEL;
//send notif
notManager.notify(1, notif);
}
What you need to do is set a pending intent -
which will be invoked when the user clicks the notification.
(Above you just started an activity...)
Here's a sample code :
private void createNotification(String text, String link){
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(text);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// pending implicit intent to view url
Intent resultIntent = new Intent(Intent.ACTION_VIEW);
resultIntent.setData(Uri.parse(link));
PendingIntent pending = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pending);
// using the same tag and Id causes the new notification to replace an existing one
mNotificationManager.notify(String.valueOf(System.currentTimeMillis()), PUSH, notificationBuilder.build());
}
Edit 1 :
I changed the answer to use PendingIntent.FLAG_UPDATE_CURRENT for the sample purpose. Thanks Aviv Ben Shabat for the comment.
Edit 2 :
Following Alex Zezekalo's comment, note that opening the notification from the lock screen, assuming chrome is used, will fail as explained in the open issue : https://code.google.com/p/chromium/issues/detail?id=455126 -
Chrome will ignore the intent, and you should be able to find in your logcat -
E/cr_document.CLActivity﹕ Ignoring intent: Intent { act=android.intent.action.VIEW dat=http://google.com/... flg=0x1000c000 cmp=com.android.chrome/com.google.android.apps.chrome.Main (has extras) }
I have the following code that creates a notification when an SMS message is received by the phone. It displays the notification correctly; however, when the user clicks the notification, nothing happens. It should open up the SMS inbox so the user can view their message. Thanks in advance.
mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
Uri uri = Uri.parse("content://sms/inbox");
PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0, new Intent(Intent.ACTION_VIEW, uri), Intent.FLAG_ACTIVITY_NEW_TASK);
String tickerText = arg0.getString(R.string.newmsg, msgs[i].getMessageBody().toString());
Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());
notif.setLatestEventInfo(arg0, msgs[i].getOriginatingAddress(), msgs[i].getMessageBody().toString(), contentIntent);
notif.vibrate = new long[] { 100, 250, 100, 500 };
mNotificationManager.notify(R.string.recv_msg_notif_id, notif);
I got it to work. The following code shows how to accomplish this task:
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.setType("vnd.android-dir/mms-sms");