Checking pending intent availablity status not working - android

Am trying to getting pending intent. Its returning intent fine when i click notification. But it not returning any thing when i open my app through launcher icon its not returning the pending intent. I searched many links but am not get anything.
creating pending intent from service
Intent in = new Intent(this, MainActivity.class);
in.setAction(action);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
in.putExtras(bundle);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, in,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Wiphone")
.setContentText(callStatus)
.setSmallIcon(R.drawable.ic_wifi)
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.setOngoing(true)
.setPriority(Notification.FLAG_FOREGROUND_SERVICE)
.setContentIntent(pendIntent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(10, notification);
getting pending intent in activity when user launch app through launcher
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent alarmIntent = new Intent();
alarmIntent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, alarmIntent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) {
Log.d(TAG, "has pending intent");
}
else {
Log.d(TAG, "no pending intent");
}
}

Related

Android: Updating foreground service sometimes result in showing notification even when Foreground Service is killed

I have a foreground service with close button in the notification which killed the service. When service start & user immediately (within a second) trying to shutdown the service by clicking on close button on notification. When i am updating this foreground service notification based on Rest Api call response. In this scenario its a close call when user click on close & api response recieved at same time the update notification code is executing.The notification is still shown to user after Api response processing. Even i put a check whether foreground service is running or not but it still updating the notification making user believe that this foreground service is not stopped.
below is code fore creating forground service :
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
String title = "Creating ...";
intent.putExtra(TASK_ID, -1);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = myNotificationUtil.getBuilder(title, "Calling" + " ..");
mBuilder.setUsesChronometer(true);
mBuilder.setContentIntent(pendingIntent);
Intent stopIntent = new Intent(this, MyService.class);
stopIntent.setAction(MyIntent.STOP_FOREGROUND);
PendingIntent stopPlayIntent = PendingIntent.getService(this, 0, stopIntent, 0);
mBuilder.addAction(R.mipmap.ic_call_end_white_24dp, "Hang Up", stopPlayIntent);
startForeground(1, mBuilder.build());
below is update notification method :
private void updateForeGroundNotification(String status) {
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
String title = "Updating ...";
intent.putExtra(TASK_ID, task_id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = myNotificationUtil.getBuilder(title, status + " ..");
mBuilder.setUsesChronometer(true);
mBuilder.setContentIntent(pendingIntent);
Intent stopIntent = new Intent(this, MyService.class);
stopIntent.setAction(MyIntent.STOP_FOREGROUND);
PendingIntent stopPlayIntent = PendingIntent.getService(this, 0, stopIntent, 0);
mBuilder.addAction(R.mipmap.ic_call_end_white_24dp, "Hang Up", stopPlayIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
I have to just update the title of notification after api call in the Notification

Notification is not redirecting when clicked

The redirection works fine, but the redirection does not happen if the Push Notification is clicked immediately like within 2 seconds or so. The second time I made sure to wait anywhere around 10-15 seconds and that time it worked fine.
Can anyone help me out how to solve this problem.
Notification notification;
Intent notificationIntent = BMSNotificationIntent.getNotificationIntent(notificationData, mContext,
mSharedPreferencesManager, true);
notificationIntent.putExtras(rawNotificaionData);
PendingIntent NotificationPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
notificationBuilder
.setSmallIcon(R.drawable.status_icon)
.setColor(ContextCompat.getColor(mContext, R.color.transparent))
.setContentIntent(NotificationPendingIntent)
.setTicker(notificationData.getCleverTapTitle())
.setAutoCancel(isAutoCancelable())
.setContentTitle(notificationData.getCleverTapTitle())
.setContentText(notificationData.getCleverTapMessage())
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
notificationBuilder.setChannelId(NotificationChannelName.GENERAL);
/*Create a Delete Intent that will be called when user removes the notification by swiping or by clear*/
notificationBuilder.setDeleteIntent(getNotificationDeletePendingIntent());
notification = notificationBuilder.build();
notification.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
if (BMSUiUtility.isNotificationFromInbox(rawNotificaionData, mSharedPreferencesManager)) {
mNotificationManager.notify(notificationId, notification);
} else {
mNotificationManager.notify(0, notification);
}
Please use below code snip:)
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
else
Intent intent = new Intent(this, SomeActivity.class);
// Creating a pending intent and wrapping our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
// Perform the operation associated with our pendingIntent
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}

Notification OnClick Event

I want to call the Activity NotificationReceiver, when I click onto an android notification.
The notification is already shown correctly, but when I click onto the notification nothing happens.
Here is the code for the notification:
private void notification_anzeigen2(){
Log.d("Test2", "Test2 ");
Intent intent = new Intent(this, NotificationReceiver.class);
intent.putExtra("NotiClick",true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, Intent.FILL_IN_ACTION);
Notification n = new Notification.Builder(this).setContentTitle("GestureAnyWhere Hintergrundservice")
.setContentText("Bitte klicken, um den Hintergrundservice zu beenden")
.setContentIntent(pIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, n);
Log.d("Test3", "Test3 ");
}
And here is the activity:
public class NotificationReceiver extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null)
{
Log.d("Test4", "Test4");
}
else if (extras.getBoolean("NotiClick"))
{
Log.d("Test5", "Test5");
stopService(new Intent(getApplicationContext(), HintergrundService.class));
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
Thanks a lot.
Try:
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
You pass as a flag a constant that has nothing to do here, so its value can match to an undesired flag, see PendingIntent doc.
It says: "May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens."

Android bundle from pending intent always recieved as null

I tried everything suggested in stackOverflow - nothing helps.
The notification works fine, it's just loses it's bundle somehow.
here is my PendingIntent:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, LoginActivity.class);
// Intent intent = new Intent().setClass(this, LoginActivity.class);
intent.putExtra(Constants.KEY_BUNDLE_MESSAGE_FROM_NOTIFICATION,true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.setAction("dummy");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(text)
/* .setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))*/
.setAutoCancel(true)
.setContentIntent(contentIntent);
notificationManager.notify(0, noBuilder.build());
1) adding action to the Intent: intent.setAction("dummy");
2) adding setClass to the Intent: .setClass(this, LoginActivity.class);
3) Pending intent tried all flags: PendingIntent.FLAG_UPDATE_CURRENT / FLAG_CANCEL_CURRENT / FLAG_ONE_SHOT/ 0
4) tried recieving the extras in both "onNewIntent" and in "onCreate"
the extras in the recieving activity is always false
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "im in login from notification! "+ intent.hasExtra(Constants.KEY_BUNDLE_MESSAGE_FROM_NOTIFICATION));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Intent intent = getIntent();
Log.d(TAG, "im in login from notification! "+ intent.hasExtra(Constants.KEY_BUNDLE_MESSAGE_FROM_NOTIFICATION));
}
any ideas?
Thanks!
In onNewIntent add : setIntent(intent)

Intent does not refresh in GCMListenerService

I am adding functionality to receive push notifications in Android. I have added a subclass of GcmListenerService and have overriden the onMessageReceived method and am building a notification with action buttons. My code is as follows:
GCMListenerService
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, PushActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("pushMessage", message);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Intent acceptIntent = new Intent(this, GcmBroadcastReceiver.class);
acceptIntent.putExtra("pushMessage", message);
acceptIntent.putExtra("action", Constants.ACTION_ACCEPT);
acceptIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(this, 0, acceptIntent, 0);
Intent denyIntent = new Intent(this, GcmBroadcastReceiver.class);
denyIntent.putExtra("action", Constants.ACTION_DENY);
denyIntent.putExtra("pushMessage", message);
denyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent denyPendingIntent = PendingIntent.getBroadcast(this, 0, denyIntent, 0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Authenticator")
.setContentText("You have initiated a transaction")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(NotificationCompat.PRIORITY_MAX)
.addAction(R.drawable.ic_media_play, "Accept", acceptPendingIntent)
.addAction(R.drawable.ic_media_pause, "Deny", denyPendingIntent)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
The issue which I am facing is that no matter what notification I send, I am receiving the same message always when I try to use the action buttons. When I try to open the activity, the correct messages gets read. I am reading the message in the GcmBroadcastreceiver and it is always the same. It seems that the intent is the same and does not get recreated. Is there something I am doing wrong?

Categories

Resources