Sending Notification between Emulators - java

As already asked by one person I would like to know, if someone could help us in Sending and recieving notification between 2 emulators.
For example there is a function which sends notification, but not to the same emulator (say 5554) but to another emulator (say 5556) so that receiver could check the notification and trigger the onClick listener to accept or reject the Notification
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,msg,System.currentTimeMillis());
notification.setLatestEventInfo(this, msg, details, null);
int num = 0;
manager.notify(num, notification);

Notification cant be sent to another device, you can only notify your own device by your app. if you want to show any notification in another device you have to write codes for it in your app which runs on the other device and trigger the notification using sms or some web service or the push mesassage system.
if you want to communicate with other device for notification.. use the PUSHMESSAGE.. check GCM http://developer.android.com/google/gcm/index.html

Related

Show notifications on lock screen of Xiaomi devices

Push notifications not shows on lock screen of Xiaomi devices.
I've tried to use VISIBILITY_PUBLIC in notification builder and channel but it doesn't work.
The problem is that Xiaomi devices has special permission in apps notifications settings which permits to show notification at lock screen. But this permission is turn off by default. But in some apps like "Telegram" this permission is on by default after installation from google play, I can't find the solution how to do that.
Not sure if this helps, but I had a similar problem an a Huawei Device (API 29).
I wanted to use NotificationManager.IMPORTANCE_LOW on my NotificationChannel, but when I was trying to send Notifications on this Huawei Device, they where not Visible on the Lock Screen.
I figured out that there is an App Notification Option on this Huawei Device to use "gentle notifications". Those Notifications are not shown on the Lock Screen and this Option is turned on by Default if your Channel uses IMPORTANCE_LOW or below.
Changing the Importance of the Channel to IMPORTANCE_DEFAULT was solving my Problem.
Since I wanted IMPORTANCE_LOW because I dont wanted an Notification Sound, I just had to do a little workaround and set setSound(null, null) and setVibrationPattern(null).
NotificationChannel nChannel1 = new NotificationChannel(CHANNEL_1_ID, "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
nChannel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
nChannel1.setSound(null, null);
nChannel1.setVibrationPattern(null);
nChannel1.setDescription("Description");
nManager = context.getSystemService(NotificationManager.class);
nManager.createNotificationChannel(nChannel1);
Notification notification = new NotificationCompat.Builder(applicationContext, CHANNEL_1_ID)
.setContentTitle("title")
.setContentText("text")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
nManager.notify(1, notification);

How to retrieve the id of the received local notification in android studio?

I am creating an app that includes scheduled notifications that are uniquely identified by the Notification_ID. The response to each notification is different, but I don't to create multiple Channel_ID (same channel_ID for all notifications). When I receive a notification, how can I retrieve the Notification_ID?
Let's say I triggered 3 notifications with different Ids: (same channel ID)
1- notificationManager.notify ("100", builder.build()); //first notification with ID 100
2- notificationManager.notify ("200", builder.build()); //second notification with ID 200
3- notificationManager.notify ("300", builder.build()); //third notification with ID 300
When I receive all 3 notifications and press on one of them, how can I retrieve the ID of that notification?
If anyone can help, it would be great.

notification displayed differently depending on whether app is in foreground or background

I'm having trouble with notifications on Android. I use the code below to generate a notification on the device whenever a GCM message is received by my app. However, it's producing unexpected results.
public class MyGcmListenerService extends GcmListenerService implements Constants {
private static final String TAG = "MyGcmListenerService";
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("msg");
sendNotification(message);
}
private void sendNotification(String message) {
NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.gcm_notification_channel_name);
String description = getString(R.string.gcm_notification_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(GCM_NOTIFICATION_CHANNEL_ID, name,
importance);
channel.setDescription(description);
channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
channel.enableVibration(true);
channel.setSound(defaultSoundUri,
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,
GCM_NOTIFICATION_CHANNEL_ID)
.setContentText(message)
.setContentTitle("My Title")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_notification);
notificationManager.notify(0, notificationBuilder.build());
}
}
If a GCM message is received whilst the app is open (not just running, but actually open on the screen), then the resulting notification has only a title, and doesn't show the message. This screenshot demonstrates this case. Further, if I send multiple GCM messages to the device whilst the app is in the foreground, only one is displayed.
If a message is received whilst the app is either closed or running in the background, the resulting notification shows only the message, and has no title. This screenshot shows the two messages side-by-side - the bottom one was received with the app in the foreground, the top was received with the app in the background. If multiple messages are received whilst the app is in the background, all are displayed (in contrast to what happens when the app is in the foreground). This screenshot shows that multiple messages are displayed when they're received with the app in the background.
Also, the notification only appears as heads-up when the app is in the foreground.
Finally, if received in the foreground, the resulting notification does nothing when tapped. However, if received in the background, the notification opens the app when tapped. Not really bothered by this, just thought it might be indicative of the problem.
FYI: when testing, I tried both keeping the GCM message the same every time, as well as varying it. Both scenarios gave the same result.
What I'd like to figure out:
How to get both the title and message to display regardless of whether app is in foreground or not. This is the most important.
How to get the notification to appear as heads-up when the app is in the background.
Just to pre-empt any responses saying not to abuse heads-up, it's the most important feature of the app (the app must notify users of certain events in real-time), according to our users.
Update:
Bas van Stein's answer allowed me to figure out why the either only the title or message was displayed.
As he correctly pointed out, when the app is in the background, GCM messages are handled by the system. This drove me to inspect the script that is used to send messages by the backend. I realised that the person who wrote this script had sent the message within the title field of the notification field of the GCM message, and there was no body field. So I corrected this issue, and the notifications displayed correctly (for app in background).
This also allowed me to realise that the line String message = data.getString("msg"); in onMessageReceived was returning null. I changed the method as follows:
public void onMessageReceived(String from, Bundle data) {
Bundle notification = data.getBundle("notification");
String title = notification.getString("title");
String message = notification.getString("body");
sendNotification(title, message);
}
Then I added title as a parameter to sendNotification and changed the line that sets the notification title to: .setContentTitle(title). Now notifications are displayed correctly when the app is in the foreground.
Further, I added a static int to the class that I use as the notification ID (incremented every time), so now multiple notifications display correctly.
Still not solved:
I'm still unable to have notifications appear as heads-up when the app is in the background. I tried adding "priority": "high" to the GCM message notification payload, but this had no effect - apparently this is the default for a GCM notification anyway.
This answer will be only a part of the complete solution but here we go:
First issue, the background and foreground notifications seem to be generated by two different functions, you can test this by applying a break point in your code and attach the debugger. You will likely see that the background notification is not triggering the break point in this code. Perhaps you miss a manifest service?
Second issue, that only one notification is being shown is because of this line:
notificationManager.notify(0, notificationBuilder.build());
The 0 here is the notification id, if you create multiple notifications with the same id it will overwrite the notification instead of showing a new one.
Third issue, that the application is not opened on notification tab, is because there is no intent attached to the notification you generate in your code.
You can use something like this for an intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pIntent);
The intent is being called when you click on the notification, this could be any intent so you can open a special activity for example.
Hope this brings you in the correct direction.

How to clear delayed unreceived sensor data from sendbroadcast using a switch?

I'm developing an app where sensor data detected on android wear are being sent to mobile through sendbroadcast. There's a switch in the mobile app to start and stop both the sensor detection on android wear and the sensor data sending process.
The problem is, there's a delay from where the data are being sent from android wear until the the data can be received on mobile using broadcastreceiver. So whenever I press the switch to stop the service and then press it again to start the process again, the mobile will continue to receive the leftover data from the previous session before it receive the data from the new session.
Is there any way I can clear all the broadcast data when clicking the stop switch, so when I press the start switch it will only receive the new data from this session?
You can use ordered boradcast :
void sendOrderedBroadcast (Intent intent,
String receiverPermission)
Broadcast the given intent to all interested BroadcastReceivers,
delivering them one at a time to allow more preferred receivers to
consume the broadcast before it is delivered to less preferred
receivers. This call is asynchronous; it returns immediately, and you
will continue executing while the receivers are run.
And then you can abortBroadcat when you want :
void abortBroadcast ()
Sets the flag indicating that this receiver should abort the current
broadcast; only works with broadcasts sent through
Context.sendOrderedBroadcast. This will prevent any other broadcast
receivers from receiving the broadcast. It will still call
onReceive(Context, Intent) of the BroadcastReceiver that the caller of
Context.sendOrderedBroadcast passed in.
This method does not work with non-ordered broadcasts such as those
sent with Context.sendBroadcast

Android notifications - push notifications for multiple users

I am writing an app in which you can create and invite multiple users to an event. I would like to have the app send notifications to users when they are invited to an event. I am calling the following in my button onClick method to send notifications when an event is created.
private void addNotification() {
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("Hello").setContentText("This is a notification").
setContentTitle("notif").setSmallIcon(R.mipmap.ic_launcher).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
This works, however this only sends the push notification to me when the event is created. I'm not sure how to have it send to the multiple people invited to the event. I have a list of the user emails I want to notify. How can i change this code to send a notification to multiple users?
for this you can use google cloud messaging. It will help you to broadcast the notification to multiple users
https://developers.google.com/cloud-messaging/android/client

Categories

Resources