简体   繁体   English

推送通知的组播

[英]How to do multicast of push notification?

I am able to send notification one device at a time. 我可以一次将通知发送到一台设备。 I need to change php code or java code? 我需要更改php代码或java代码吗? Below is my code: 下面是我的代码:

Create a notification to inform the user that server has sent a message. 创建一个通知,以通知用户服务器已发送消息。

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      

}

} }

THe Java code on the Android app has nothing to do with sending multicast messages. Android应用程序上的Java代码与发送多播消息无关。 You have to change the PHP code on your server. 您必须在服务器上更改PHP代码。

If you want to send the same message to multiple devices you should send an HTTP request with application/json content type of the form : 如果要将同一条消息发送到多个设备,则应发送以下形式的application/json内容类型的HTTP请求:

{
  "registration_ids" : ["reg-id1","reg-id2",...],
  "data" : {
    ...
  },
}

This allows you to send the same message to up to 1000 devices in a single HTTP request. 这使您可以在单个HTTP请求中将相同的消息发送到多达1000个设备。

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

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