简体   繁体   中英

Sending notifications to all devices

I have an app in which markers can be added to the map using the Google Maps API, I'm trying to send a notification to all devices with the app installed when a new marker is added, this works for the device that is currently using the app but not my other device which does not have the app loaded, is there something else I have to do to register it with other devices?

Here is the code for connecting to the server and adding the marker, which calls the showNotification method:

try
{
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://***.***.***.**/markerLocation/save.php");
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();

    is = entity.getContent();

    String msg = "Data entered successfully";

    //The method call that makes the alert notification
    ShowNotification(name);
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}

and here is the code for creating the alert:

public  void ShowNotification(String name)
{
    // define sound URI, the sound to be played when there's a notification
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // intent triggered, you can add other intent for other actions
    Intent intent = new Intent(GoogleMapsActivity.this, NotificationReceiver.class);
    PendingIntent pIntent = PendingIntent.getActivity(GoogleMapsActivity.this, 0, intent, 0);

    // this is it, we'll build the notification!
    // in the addAction method, if you don't want any icon, just set the first param to 0
    Notification mNotification = new Notification.Builder(this)


            .setContentTitle(name)
            .setContentText(name + " has added a marker in your area")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .setSound(soundUri)

            .addAction(0, "View", pIntent)
            .addAction(0, "Remind", pIntent)

            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // If you want to hide the notification after it was selected, do the code below
    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}    

First you will need a Push service to warn other devices of your new marker, then you will need a BroadCastReceiver to receive the push message and emit the Notification on all devices that received it, I would love to explain this and write some example code for you but its widely explained in Android Docus so why reinvent the wheel?

Look at this page, it has everything u need:

Google Cloud Messaging GCM

I think you are not grasping how the two notifications type functions. The way you would do this is by storing on your server the device id of all your users that have requested to receive the notifications. Then you initiate the notification for all devices from the server not from the app. Notification initiated from the app are only to display a message on the device outside of our app's UI

Take a look at this: https://developer.android.com/google/gcm/index.html

What you need is some kind of support for push notifications, an obvious choice for Android is Google Cloud Messaging (GCM).

Since you have a server available, you could tailor the server-side yourself for managing which devices that receive the notifications (plenty of tutorials out there).

If you are in a hurry and just want to get stuff working, you can use parse.com. They allow you to send push messages to 1 mil unique devices (through GCM) for free. The upside here is that they make it easier to setup and filter which devices that should receive the notifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM