简体   繁体   English

向所有设备发送通知

[英]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? 我有一个可以在其中使用Google Maps API将标记添加到地图的应用程序,我正在尝试向添加了新标记的安装了该应用程序的所有设备发送通知,这适用于当前正在使用的设备该应用程序,但没有加载该应用程序的其他设备,我是否还需要做其他事情才能将其注册到其他设备?

Here is the code for connecting to the server and adding the marker, which calls the showNotification method: 这是用于连接到服务器并添加标记的代码,该代码调用showNotification方法:

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? 首先,您将需要一个推服务来警告其他设备您的新标记,然后您需要一个BroadCastReceiver来接收推消息并在接收到该消息的所有设备上发出通知,我很乐于解释这一点并为您,但它在Android Docus中得到了广泛的解释,那么为什么要重新发明轮子呢?

Look at this page, it has everything u need: 查看此页面,它具有您需要的一切:

Google Cloud Messaging GCM 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. 您将通过在服务器上存储所有请求接收通知的用户的设备ID来执行此操作。 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 看看这个: 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). 您需要某种对推送通知的支持,Android的一个明显选择是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. 如果您赶时间并且只想让工作正常进行,可以使用parse.com。 They allow you to send push messages to 1 mil unique devices (through GCM) for free. 它们允许您免费将推送消息发送到100万个唯一设备(通过GCM)。 The upside here is that they make it easier to setup and filter which devices that should receive the notifications. 这样做的好处是,它们使设置和过滤应接收通知的设备变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C#.net 中向多个设备(但不是所有设备)发送 FCM 通知 - Sending FCM Notifications to multiple devices (but not all devices) in C#.net 在没有 Firebase 控制台的情况下向所有设备发送推送通知 - Sending Push Notifications to all devices without Firebase Console 使用FCM向所有订阅主题的设备发送推送通知(批量)时出现缩放问题 - Scaling issue while sending push notifications (in bulk) to all the devices subscribed to a topic using FCM 使用Parse将推送通知发送到Android设备 - Sending push notifications to Android devices using Parse 向半径为5米的移动设备发送通知 - Sending Notifications to Mobile Devices with in a 5 Meter Radius 设备离线时接收所有推送通知 - Receive all the push notifications when devices are offline 通过主题将推送通知发送到所有设备 - Send push notifications via Topic to all devices 一台服务器向Android和iOS设备发送推送通知 - One server sending push notifications to Android and iOS devices 使用GCM向多个Android设备发送推送通知 - sending push notifications to multiple android devices using GCM Android GCM推送通知-将消息发送到一组设备 - Android GCM push notifications - sending messages to a set of devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM