Push notification don't open activity if screen has pin/graphical key - android

Bug is present on phones with screen pin/graphical key lock. Without it works just fine.
When user taps on push notification in starts main activity and thats all, if there is no pin or graphical key it starts needed activity.
This is my GCM listener code:
#Override
public void onMessageReceived(String from, Bundle data) {
String id = data.getString("news_id");
String message = data.getString("message");
Random random = new Random();
Intent intent = new Intent(this, SpecificNewsActivity.class);
intent.putExtra("newsId", id);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, random.nextInt(), intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(random.nextInt(), notificationBuilder.build());
}
Any ideas how to fix this ?
Edit: Looks like this problem is present only on Chinese phones (Meizu, Xiaomi).

If anyone else face this. The question was solved simply - requests to the server was moved from Activity which has fragment container to the fragment itself. Without this on Xiaomi and Meizu activity stopped on the stage of fragment transaction in the frame layout.

Related

Not receiving notification of the next user

I have made a chat app in firebase and users are being notified when a new message comes by the below codes.
private void sendNotification(String title, String message, String receiver, String receiverUid, String firebaseToken) {
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("name",title);
intent.putExtra(Const.ARG_RECEIVER, receiver);
intent.putExtra(Const.ARG_RECEIVER_UID, receiverUid);
intent.putExtra(Const.ARG_FIREBASE_TOKEN, firebaseToken);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
char[] ch = firebaseToken.substring(0,3).toCharArray();
String a = "";
for (char aCh : ch) {
a = a + Integer.toHexString(aCh);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.parseInt(a), intent,
PendingIntent.FLAG_ONE_SHOT);
// Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_noti)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(a), notificationBuilder.build());
}
I want to receive different notification when a different user sends a message but for the same user, the notification should be updated. I tried to set unique id by taking the Unicode of first 3 letters of Firebase token of the user but then also it is showing only one notification. If I chat with 2 people the user with whom I have started chat first shows notification but not the second one.
Please have a look and help. Thanks
Just change FLAG_ONE_SHOT to FLAG_UPDATE_CURRENT.
FLAG_ONE_SHOT :
Flag indicating that this PendingIntent can be used only once.
FLAG_UPDATE_CURRENT :
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

Which overridden method of Activity runs when click on notification?

I have MainActivity which I want to open when user clicks on a notification.
My code is below.
Manifest.xml:
<activity android:name=".activity.MainActivity"></activity>
Notification code:
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
I want to know:
Which overridden method is called when my app is closed and I click on a notification?
Which overridden method is called when my app is running and I click on a notification?
Good Monday Morning!
It's actually a bit more complex: when your notification is clicked, the pending Intent you created is executed and, as a result, the activity which was assigned in your notification (in your case the MainActivity)
So the activity starts as a normal activity and if you need to pass some parameters, just add extra to the Intent before transforming it in a Pending Intent
OnMessage will always get called whenever notification came to mobile, even if app is closed or running or in background.
protected override void OnMessage(Context context, Intent intent) {
// Your code here
}

Android notification in background continue previous intent

sendNotification('Hello', 0, 1);
private void sendNotification(String message, int id, int notification_num) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, Main.class);
intent.setData(new Uri.Builder().scheme("data").build());
intent.putExtra("id", id);
intent.putExtra("noti_number", notification_num);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Main.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App Title")
.setContentText(message)
.setSmallIcon(R.drawable.notif)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationBuilder.setNumber(notification_num);
nm.notify(id, notificationBuilder.build());
}
When application is opened intent.putExtra works fine, but when I close app (kill proccess), notification is coming but not with it extra than with new intent extra.
Problem is also with multiple notification, when app is closed, there are no grouping like I want it to be.
notificationBuilder.setNumber is always 0 in background.
Need help how to run app always in background or how to continue previous intent.
I have done it using GcmIntentService & GcmBroadcastReceiver, for more info click on this link.

Android Push notification is not working on Model number- vs450pp having version 4.4.2

I am getting push notification on Android phone on given model number but when I clicked on the notification, It disappear and doesn't open app. I would be grateful If anyone get me out from that problems.
Try Following Code May be work for you .
Intent intent = new Intent(this, MainActivity.class); //define your application activity name which you want to open.
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Application Title")
.setContentText(message)
.setContentIntent(pIntent)
.setSound(soundUri); //This sets the sound to play
notificationManager.notify(0, mBuilder.build());

NotificationCompat and setFullScreenIntent()

I was wondering if anyone has some experience with with this type of Notification.
In my use case I want to trigger a notification from my Service and than it should open a fullscreen video. The method setFullScreenIntent look just the right thing for this problem because in the documentation it writes:
An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time.
So it says it works like an incoming phone call. That means even if my phone is asleep I should get to see the notification in full screen.
But in my case I just get the heads-up notification and if I click on it it opens the activity. Even thought in the docs they mention something about this behavior ...
On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.
... I want to know how to automatically open the activity when the notification is triggered. Just like the incoming phone call screen.
This is my code from the service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
return Service.START_STICKY;
}
try to notify notification with setFullScreenIntent at onCreate
#Override
public int onCreate() {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
Try removing
.setContentIntent(contentIntent)

Categories

Resources