Notification with Multiple actions - android

I have this code to make the actions:
Intent playIntent = new Intent(Intent.ACTION_VIEW);
playIntent.setDataAndType(uri, path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent play = PendingIntent.getActivity(context, 1, playIntent, 0);
mBuilder.addAction(R.mipmap.ic_play_arrow_black_48dp, "", play);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType(path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent share = PendingIntent.getActivity(context, 2, shareIntent, 0);
mBuilder.addAction(R.mipmap.ic_share_white_48dp, "", share);
Intent doneIntent = new Intent(context, NotificationCloser.class);
doneIntent.putExtra("notificationId", notificationId);
PendingIntent done = PendingIntent.getBroadcast(context, 3, doneIntent, 0);
mBuilder.addAction(R.mipmap.ic_done_black_48dp, "", done);
And this is my broadcast receiver
public class NotificationCloser extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra("notificationId", 0);
Log.i("MyInfo", "NotificationCloser.onReceive(" + id + ")");
MainActivity.mNotifyManager.cancel(id);
}
}
When I click in play or share button, it does according the function, opens the default app to view images or videos but doesn't close the notification. When I click in the done button, ONLY in the first time i receive the id correctly, after the first time it gets the id sent in the first time
Can someone help me?

I solved the problem of the broadcast by using random request codes, also added the flag PendingIntent.FLAG_UPDATE_CURRENT. I used in all because sometimes the share bugged and used another image.
PendingIntent play = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, playIntent, 0);
PendingIntent share = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, shareIntent, 0);
PendingIntent done = PendingIntent.getBroadcast(context, new Random().nextInt(150) * 37, doneIntent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer was posted as an edit to the question Notification with Multiple actions by the OP Rodrigo Butzke under CC BY-SA 3.0.

Related

AlarmManager with BroadCastReceiver: Programed two alarms but received only one

I have two alarms and a broadcastreceiver:
AlarmManager am2=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, MyReceiverAlarm.class);
intent2.putExtra("name", "juan");
PendingIntent pi2 = PendingIntent.getBroadcast(context, 0, intent2, 0);
am2.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+1000, pi2);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReceiverAlarm.class);
intent.putExtra("name", "jose");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+8000, pi);
However, I'm always receiving the first one, only. I always get "juan" and the alarm is fires after 8 seconds.
I don't understand the reason...If i have two alarms why fires one ?
My BroadcastReceiver:
Toast.makeText(context, extras.getString("nombre") + "", Toast.LENGTH_LONG).show();
String name=extras.getString("nombre");
if (extras != null && name.equals("juan") {
String callForwardString = PHONE ;
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri uri2 = Uri.fromParts("tel", callForwardString, "#");
intentCallForward.setData(uri2);
intentCallForward.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentCallForward);
} else {
...
}
Is not possible to program two alarms?
Thanks in advance
Try to change the request code (second parameter passed to getBroadcast():
PendingIntent pi2 = PendingIntent.getBroadcast(context, 2, intent2, 0);
PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, 0);

How to pass thread id through pending intent

How to pass thread id through pending intent to open exact message conversation, like when we get a message with click notification and see the conversation
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ConvActivity.class);
intent.putExtra(ConvActivity.THREAD_ID_EXTRA, threadId);
intent.putExtra(ConvActivity.THREAD_NAME_EXTRA,listItem.getContactName());
intent.putExtra(ConvActivity.THREAD_IMAGE_EXTRA, uri);
intent.putExtra(ConvActivity.THREAD_NUM_EXTRA,listItem.getAddress());
You are passing another intent to your pending intent - pay attention.
Intent intent = new Intent(context, ConvActivity.class);
intent.putExtra(ConvActivity.THREAD_ID_EXTRA, threadId);
intent.putExtra(ConvActivity.THREAD_NAME_EXTRA,listItem.getContactName());
intent.putExtra(ConvActivity.THREAD_IMAGE_EXTRA, uri);
intent.putExtra(ConvActivity.THREAD_NUM_EXTRA,listItem.getAddress());
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
try this code
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ConvActivity.class);
intent.putExtra(ConvActivity.THREAD_ID_EXTRA,
threadId.longvalue);// change here
intent.putExtra(ConvActivity.THREAD_NAME_EXTRA,listItem.getContactName());
intent.putExtra(ConvActivity.THREAD_IMAGE_EXTRA, uri);
intent.putExtra(ConvActivity.THREAD_NUM_EXTRA,listItem.getAddress());

Notification with multiple buttons

I need to display notification with two buttons. Different operation need to perform for each button.so for that I have written following code but when I'm getting multiple notification delete action is not performing.
Random NOTIFICATION_ID = new Random();
int CANCELNOTIFICATIONID = NOTIFICATION_ID.nextInt();
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Log.i("******* Service6", "" + msg);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(GcmIntentService.this, LoginActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(GcmIntentService.this, 0, intent, 0);
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
deleteIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Delete loops");
Trace.i(TAG, "Looptype Delete loop");
deleteIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
deleteIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
deleteIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pDeleteIntent = PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent archiveIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
Trace.i(TAG, "Looptype Archive loop");
archiveIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Archive loops");
archiveIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
archiveIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
archiveIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pArchiveIntent = PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle("Sample");
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mPushtext));
notificationBuilder.setContentText(mPushtext);
notificationBuilder.setSound(soundUri);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notificationBuilder.setAutoCancel(false);
// notificationBuilder.setContentIntent(pArchiveIntent);
Log.i("GCMIntent Srevice5", "" + msg);
notificationBuilder.setOngoing(true);
mNotificationManager.notify(CANCELNOTIFICATIONID, notificationBuilder.build());
How to solve this issue.
You need to use different requestCode in Pending intent
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 1, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 2, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
In the below way i have solved it.
in the intent i have given two differentactivities
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteLoopActivity.class);
Intent archiveIntent = new Intent(GcmIntentService.this, ArchiveLoopActivity.class);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, deleteIntent, 0));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, archiveIntent, 0));
The above code solves my problem
Thanks all

GCM start background operations (make post to server) on notification click

I'm using Google Play Services lib for GCM and all works great.
Now I implement 2 buttons (actions) in my notificationCompat.Builder:
- One open app.
- Other one delete some unread messages: for this operation I post some dato to server.
How can I launch a post to my server on notification action click without open any activity in front of user?
Thank you very much!!!!!
Here my code:`Intent openIntent = new Intent();
Intent cancelIntent = new Intent();
if (has1Id) {
//go details!
openIntent = new Intent(this, NotificationActivity.class);
openIntent.putExtra("id", ids.get(0));
//cancell intent: start async operation in background
cancelIntent = new Intent(this, HomeActivity.class);
cancelIntent.putExtra("id", ids.get(0));
}else{
//go home!
openIntent = new Intent(this, HomeActivity.class);
//cancell intent: start async operation in background
cancelIntent = new Intent(this, HomeActivity.class);
cancelIntent.putExtra("id", ids);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelNotificationIntent = PendingIntent.getActivity(this, 0,
cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long[] vibrationPattern = {0, 200, 800, 200, 100, 500};
Uri soundUri = Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.jingle1);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title) //set title
.setContentInfo(numNotRead) //number of unread content
.setAutoCancel(true)
.setContentText(msgs.get(0)+"...")
.setVibrate(vibrationPattern)
.setSound(soundUri)
.setLights(Color.rgb(255, 165, 00), 800, 800)
.setContentIntent(contentIntent).addAction(R.drawable.ic_notification, getString(R.string.open_app), contentIntent)
.setContentIntent(cancelNotificationIntent).addAction(android.R.drawable.ic_delete, getString(R.string.mark_read), cancelNotificationIntent);
if (has1Id) {
NotificationCompat.BigTextStyle bigTextStyle =new NotificationCompat.BigTextStyle().bigText(msgs.get(0));
mBuilder.setContentTitle(getString(R.string.notification_new_content));
//get only 1 long text: the description of content
mBuilder.setStyle(bigTextStyle);
}else{
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(getString(R.string.notification_new_contents));
for (int i=0; i < msgs.size(); i++) {
inboxStyle.addLine(msgs.get(i));
}
mBuilder.setStyle(inboxStyle);
}
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());`
You should use another class as the intent receiver for cancelIntent, like a BroadcastReceiver and handle the intent there with no UI.
public class MyIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context _context, Intent _intent) {
if (_intent.getAction().equals(MY_INTENT)) {
// TODO Handle a notification
}
}
}
You also can use a Activity with no UI as the intent, but I think previous option is better.
More info:
Android Activity with no GUI

Open a calendar from a widget app

I want to open a calendar (for instance, google calendar) from my widget app. I did something like that:
Intent intent2 = new Intent();
//Froyo or greater
ComponentName cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
intent2.setComponent(cn);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1,
intent2, 0);
updateViews.setOnClickPendingIntent(R.id.calendar, pendingIntent2);
But it doesn´t work. Any help?
Thank you very much
Finally it works:
Intent intent2 = new Intent();
ComponentName cn = new ComponentName("com.android.calendar",
"com.android.calendar.LaunchActivity");
intent2.setComponent(cn);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1,
intent2, 0);
updateViews.setOnClickPendingIntent(R.id.calendar, pendingIntent2);
and in the manifest:
<activity
android:name="com.android.calendar.LaunchActivity">
</activity>

Categories

Resources