簡體   English   中英

向所有設備發送通知

[英]Sending notifications to all devices

我有一個可以在其中使用Google Maps API將標記添加到地圖的應用程序,我正在嘗試向添加了新標記的安裝了該應用程序的所有設備發送通知,這適用於當前正在使用的設備該應用程序,但沒有加載該應用程序的其他設備,我是否還需要做其他事情才能將其注冊到其他設備?

這是用於連接到服務器並添加標記的代碼,該代碼調用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();
}

這是用於創建警報的代碼:

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);
}    

首先,您將需要一個推服務來警告其他設備您的新標記,然后您需要一個BroadCastReceiver來接收推消息並在接收到該消息的所有設備上發出通知,我很樂於解釋這一點並為您,但它在Android Docus中得到了廣泛的解釋,那么為什么要重新發明輪子呢?

查看此頁面,它具有您需要的一切:

Google Cloud Messaging GCM

我認為您沒有掌握兩個通知類型的功能。 您將通過在服務器上存儲所有請求接收通知的用戶的設備ID來執行此操作。 然后,您從服務器而不是從應用程序啟動所有設備的通知。 從應用啟動的通知僅在應用的用戶界面之外的設備上顯示消息

看看這個: https : //developer.android.com/google/gcm/index.html

您需要某種對推送通知的支持,Android的一個明顯選擇是Google Cloud Messaging(GCM)。

由於您有可用的服務器,因此您可以自己調整服務器端,以管理接收通知的設備(那里有大量的教程)。

如果您趕時間並且只想讓工作正常進行,可以使用parse.com。 它們允許您免費將推送消息發送到100萬個唯一設備(通過GCM)。 這樣做的好處是,它們使設置和過濾應接收通知的設備變得更加容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM