Query regarding Android push notifications - java

I have an Android app with a working push notification set up. It works currently like this:
User gets a push notification
When the user taps on the push notification, he is taken to a screen which has the list of all the notifications. It is basically a list view which has all the notifications.
Now, want to implement the following:
a. When a new notification comes to the user, it has to be displayed on the list view, irrespective of whether a user taps on it.
b. I have to indicate the number of notifications that user has received on the app icon. For instance, if you get a message in whats app, it displays the number of messages on the app icon.
Could anybody let me know what is the best possible way to implement these functionality? If anybody could point me to tutorials/references, it would be very helpful.
Thanks!

a. Launch an activity from the receiver(GCM/C2DM receiver) when the push notification arrives
// Your C2DM receiver (for GCM check the Android Documentation)
public void onReceive(final Context context, final Intent intent)
{
if (intent.getAction().equals(com.google.android.c2dm.intent.RECEIVE))
{
//start your list view activity of notifications
Intent i = new Intent();
i.setClassName("com.test", "com.test.NotificationsActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
b. Check this.

Related

react native data only fcm on android: start foreground when app is killed

I'm building a react native app which should handle push notifications.
I'm using react-native-notifications to handle the notification logic, and notifee to show the received notifications. But I have a few problems.
There are 3 'states' in which the app is, when a notification should be handled: app in foreground, background or killed.
When the app is in foreground or in background, I can "catch" the notifications with the event handlers from react-native-notifications and based on the payload, create a notification with notifee to display.
The problem is when the app is killed.
I found a pull request of react-native-notifications (see PR here) that would "read" data-only messages on android when the app is killed.
This works, I console.log the output and it shows up in logcat.
My question now is: how can I create a custom notification with notifee when the app is killed? So no react native code can run?
In my index.js I have the following:
AppRegistry.registerHeadlessTask(
"JSNotifyWhenKilledTask",
() => {
return async (notificationBundle) => {
console.log('[JSNotifyWhenKilledTask] notificationBundle', notificationBundle);
}
}
);
the notification is shown in logcat, but now I want to add notifee to this to create a notification. But then I get the following error (in logcat):
android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent
The code that is a problem is this one:
private void notifyReceivedKilledToJS() {
Bundle bundle = new Bundle(mNotificationProps.asBundle());
Intent service = new Intent(mContext.getApplicationContext(), JSNotifyWhenKilledTask.class);
service.putExtras(bundle);
mContext.getApplicationContext().startService(service);
}
I modified the code to this, but now I get another error:
private void notifyReceivedKilledToJS() {
Bundle bundle = new Bundle(mNotificationProps.asBundle());
Intent service = new Intent(mContext.getApplicationContext(), JSNotifyWhenKilledTask.class);
service.putExtras(bundle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mContext.startForegroundService(new Intent(mContext, JSNotifyWhenKilledTask.class));
} else {
mContext.startService(new Intent(mContext, JSNotifyWhenKilledTask.class));
}
}
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false
I found this question on SO addressing this problem.
I changed the priority of the notification to "high", and now I get this final error:
android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground():
I'm guessing I should start this Service.startForeground() somewhere in my java code, but I really have no clue where.
Does anyone know a solution on how to "read" a data-only notification on android when the app is killed, and to run some JS code (notifee) to display a notification?
I'm not very good at java btw, any guidance would be welcome.

how can i launch my app or an activity on notification received when app is in background or killed?

I have a requirement for my app. I have to launch my app when I received a Notification (specific notification type) when my app is in background or killed state. I didn't find any solution for background. I'm using FCM for notifications.
Actually my app support calling functionality from web side to mobile and when admin initiate call then I received a FCM push notification. when my app is foreground than it's work fine but when it is in background, than I received notification but for launch app I have to click on notification. but I need to launch app immediately. Have any one have any solution?
Try this one.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
startActivity(new Intent(this, MainActivity.class));
}
}
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
If you are sending the notification from console, then use the particular key to send the notification from the Advanced Option features.
if (getIntent().getExtras() != null) {
// Call your NotificationActivity here..
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
startActivity(intent);
}

Bring app in background to front

I am making an app that gives an alarm when you are close to a gps position. now when a user presses the home button this app get send to the back. When the users enters the region to trigger the alarm, the alarm triggers but the app isn't send to the foreground.
How can i do this?
I tried this
Intent intent = new Intent(context, TrainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
in the function of the alarm, but this does nothing? (Trainactivity is the class that runs on the background and the class that needs to be on the foreground)
The REORDER_TO_FRONT flag only controls the stacking order on the activity history. That is not what you want. Is this what you are looking for ? - Bring application to front after user clicks on home button

populating SMS message in android

Here is the code snippet from our app now.
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" ));
sms.putExtra("sms_body", text);
startActivity(sms);
}
This works with the stock SMS app, however a tester reported that with handcent SMS they get a blank message. This prompted me to try it with Google Voice, also get a blank message there.
Is there some way I can get my message text to work with all of these other SMS apps?
We have a clipboard functionality, so a poor workaround at least would be having user push clipboard button, then use their messaging app of choice.
Android tries to reuse Intents as much as possible. In some cases when you start an intent, android doesn't create a new Intent and reuse previous intents. To avoid this set this flag of Intent:
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This flag forces Android to create a new Task with the intent's Activity on top of stack.
When you pass this intent , this will be delivered to all those apps which have registered an activity to receive it. In otherwords you have to use the intent that is registered by the application that u intent to handle your intent

A way to have my BroadcastReceiver block broadcast from system?

I am writing an Android app where the user can take pictures and I am using my own camera functionality instead of Androids built in camera software. It all works fine except I want to be able to take a picture when the user presses the hard camera button. I registered a Broadcast receiver, and it works but Android still opens its camera program over my app. Is there a way to block the built in app from receiving the broadcast?
I am posting my code below.
Any help would be greatly appreciated.
Thankyou
//Listen for camera button to be pressed
cameraButtonListener = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_CAMERA_BUTTON)){
Toast.makeText(getApplicationContext(), "Camera Button Pressed", Toast.LENGTH_SHORT).show();
}
}
};
//register broadcast receiver to listen for camera button
getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );
You can use abortBroadcast() in conjunction with android:priority set to high to "consume" the broadcast. However, this works only if the broadcast is an Ordered broadcast, and I don't know what type is ACTION_CAMERA_BUTTON. More info here.
try this code
if("android.intent.action.ACTION_CAMERA_BUTTON".equals(intent.getAction()))
in register
getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );

Categories