[英]Manipulating a GCM downstream message
I'm creating an Android application that uses GCM.我正在创建一个使用 GCM 的 Android 应用程序。 I have implemented the XMPP CCS Application Server for bidirectional functionality.我已经为双向功能实现了 XMPP CCS 应用程序服务器。 So far, upstream messaging works perfectly - The devices register themselves, obtain a token ID, and send the relevant data to the Application Server.到目前为止,上游消息工作完美 - 设备注册自己,获取令牌 ID,并将相关数据发送到应用服务器。 The App server can also parse the incoming upstream message and save it in the database.应用服务器也可以解析传入的上游消息并将其保存在数据库中。 I have also created the notification_key for a group of tokens, and even that is working fine.我还为一组令牌创建了 notification_key,即使这样也能正常工作。
Downstream messaging is also working perfectly.下游消息传递也运行良好。 I receive the message from GCM.我收到来自 GCM 的消息。 However, upon opening the notification, I want to open an Activity that calculates certain locations based on the Server's output, and display the output position on a Google Map.但是,在打开通知时,我想打开一个 Activity 根据服务器的输出计算某些位置,并在谷歌地图上显示输出位置。 How do I do that?我怎么做? I tried to create an IntentService in the onMessageReceived
function, but it doesn't work.我试图在onMessageReceived
函数中创建一个 IntentService,但它不起作用。
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.gcm.GcmListenerService;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class MyGcmListenerService extends GcmListenerService {
public LatLng midpoint;
public double lat;
public double lng;
public String location;
public PlacePicker placePicker;
public final int PLACE_PICKER_REQUEST = 1;
public GoogleMap map;
public final String SERVER_KEY = "MY_GCM_SERVER_KEY";
public double curr_lat;
public double curr_long;
@Override
public void onMessageReceived(String from, Bundle data) {
String type = data.getString("type");
//If broadcast,
if("meetup.be2015.gcm_meetup.BROADCAST".equals(type)){
//Issue request to the Google Directions and places API
//extract latitude & longitude
String latitude_str = data.getString("latitude");
String longitude_str = data.getString("longitude");
this.lat = Double.parseDouble(latitude_str);
this.lng = Double.parseDouble(longitude_str);
String dest_msg = "Destination:: "+lat+", "+lng;
sendNotification(dest_msg, "BROADCAST");
//Launch an intenservice to handle the JSON data
Intent DirectionIntent = new Intent(getApplicationContext(), Directions.class);
DirectionIntent.putExtra("current_lat",curr_lat);
DirectionIntent.putExtra("current_long",curr_long);
DirectionIntent.putExtra("target_lat",lat);
DirectionIntent.putExtra("target_long", lng);
startService(DirectionIntent); // <- HOW DO I GET THIS TO WORK?
}
else if("meetup.be2015.gcm_meetup.UNICAST".equals(type)) {
/*
* Message format of packet sent from Server:
*
* payload.put("Message", midString);
payload.put("type", "meetup.be2015.gcm_meetup.UNICAST");
payload.put("notification_key", not_key);
payload.put("Embedded_MsgID", msgID);
*
* */
Log.d("RECV_MSG","Received message from "+from+". MsgId:"+data.getString("Embedded_MsgID"));
//Create Double array
ArrayList<String> temp = new ArrayList<>();
String pos = data.getString("Message");
//Format to be the LatLng:
//"latitude: XYZ :: longitude: ABC"
String notification_key = data.getString("notification_key");
//Passing through StringTokenizer
StringTokenizer tokenizer = new StringTokenizer(pos);
while (tokenizer.hasMoreTokens()) {
temp.add(tokenizer.nextToken());
}
//Create the LatLng point
midpoint = new LatLng(Double.parseDouble(temp.get(1)), Double.parseDouble(temp.get(4)));
//Set initial radius (m)
double radius = 500;
//Use a function to map SE and NW bounds of circle
//LatLngBounds bounds = convertCenterAndRadiusToBounds(midpoint, radius);
//pack everything in an intent and send to Places.java
Intent placeIntent = new Intent(getApplicationContext(), Places.class);
placeIntent.putExtra("midpoint", midpoint);
placeIntent.putExtra("radius", radius);
placeIntent.putExtra("notification_key", notification_key);
startService(placeIntent); // <- THIS AS WELL
sendNotification(pos, "MIDPOINT");
}
}
//THIS FUNCTION WORKS PROPERLY. SENDS ME THE NOTIFICATION
public void sendNotification(String message, String type) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("GCM MESSAGE: " + type)
.setContentText(message)
.setSmallIcon(R.drawable.ic_mail_black_24dp)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
while sending notification add some data in intent like this在发送通知时,像这样在意图中添加一些数据
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("midpoint", midpoint);
intent.putExtra("radius", radius);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
and then in your mainActivity calss implement the below method然后在你的 mainActivity calss 中实现下面的方法
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("radius")) {
midpoint = intent.getStringExtra("midpoint");
radius = intent.getStringExtra("radius");
// do what ever you want with this data.
}
}
if above method does not work the also try to check intent data in onCreate like this.如果上述方法不起作用,也可以尝试像这样在 onCreate 中检查意图数据。
if (intent.hasExtra("radius")) {
midpoint = intent.getStringExtra("midpoint");
radius = intent.getStringExtra("radius");
// do what ever you want with this data.
}
i hope that will help you.我希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.