shy*_*yam 3 notifications android android-pendingintent google-cloud-messaging
我已经实现了一个简单的GCM客户端,我在更新每次收到新通知时启动的Activity时遇到此问题.每次我发送带有一组参数的通知时,只有我为第一个通知发送的参数才会出现在UI中.当我尝试更改参数时,它们不会显示(再次显示相同的数据).
这是我的GcmIntentService类
public class GcmIntentService extends IntentService {
private static final String TAG = "GcmIntentService";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
Log.d(TAG, "GCM error -> MESSAGE_TYPE_SEND_ERROR");
sendNotification("Send error: " + extras.toString(), "", "",
"", "");
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
Log.d(TAG, "GCM error -> MESSAGE_TYPE_DELETED");
sendNotification(
"Deleted Messages on server: " + extras.toString(), "",
"", "", "");
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
Log.d(TAG,
"GCM success. Received data: fromUser="
+ extras.getString("fromUser") + " message="
+ extras.getString("message") + " srcLat="
+ extras.getString("srcLat") +" srcLng="
+ extras.getString("srcLng") + " dstLat="
+ extras.getString("dstLat") + " dstLng="
+ extras.getString("dstLng"));
sendNotification(
extras.getString("fromUser") + " "
+ extras.getString("message"),
extras.getString("srcLat"), extras.getString("srcLng"),
extras.getString("dstLat"), extras.getString("dstLng"));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg, String srcLat, String srcLng,
String dstLat, String dstLng) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, ShowUser.class);
intent.putExtra("message", msg);
intent.putExtra("srcLat", srcLat);
intent.putExtra("srcLng", srcLng);
intent.putExtra("dstLat", dstLat);
intent.putExtra("dstLng", dstLng);
Log.d(TAG, "-> sendNotification -> Received parameters:\nmessage:"
+ msg + "\nsrcLat=" + srcLat + "\nsrcLng=" + srcLng
+ "\ndstLat=" + dstLat + "\ndstLng=" + dstLng);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Taxi Request Received")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Activity,它会在点击通知时启动.
public class ShowUser extends Activity {
private TextView messageTextView;
private TextView showUsernameTextView;
private TextView srcLatTextView;
private TextView srcLngTextView;
private TextView dstLatTextView;
private TextView dstLngTextView;
private SharedPreferences prefs;
private String username;
private static final String TAG = "ShowUser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_user_details);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
String srcLat = intent.getStringExtra("srcLat");
String srcLng = intent.getStringExtra("srcLng");
String dstLat = intent.getStringExtra("dstLat");
String dstLng = intent.getStringExtra("dstLng");
Log.d(TAG, "ReceivedExtras: message=" + message
+ "srcLat=" + srcLat + "srcLng=" + srcLng
+ "dstLat=" + dstLat + "dstLng=" + dstLng);
prefs = getSharedPreferences(DriverLoginActivity.USER_CREDENTIALS, Context.MODE_PRIVATE);
username = prefs.getString(DriverLoginActivity.USERNAME, null);
if(username!= null) {
messageTextView = (TextView) findViewById(R.id.messageTextView);
showUsernameTextView = (TextView) findViewById(R.id.showUsernameTextView);
srcLatTextView = (TextView) findViewById(R.id.sourceLatTextView);
srcLngTextView = (TextView) findViewById(R.id.sourceLngTextView);
dstLatTextView = (TextView) findViewById(R.id.destinationLatTextView);
dstLngTextView = (TextView) findViewById(R.id.destinationLngTextView);
showUsernameTextView.setText("Welcome " + username);
messageTextView.setText(message);
srcLatTextView.setText("Source Latitude: " + srcLat);
srcLngTextView.setText("Source Longitude: " + srcLng);
dstLatTextView.setText("Destination Latitude: " + dstLat);
dstLngTextView.setText("Destination Longitude: " + dstLng);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为它可能与此有关PendingIntent但我不确定这里发生了什么因为我PendingIntent在Android中使用s和通知相对较新.谁能告诉我发生了什么事?
在sendNotification你得到PendingIntent使用此代码:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
Run Code Online (Sandbox Code Playgroud)
第一次执行此操作时,Android将PendingIntent使用Intent您提供的新内容作为此调用的第3个参数创建新内容.以下时间执行此操作,Android将不会创建新的PendingIntent,但会返回引用PendingIntent您创建的第一个的令牌.
如果您希望Android PendingIntent在下次执行此代码时替换原件上的"extras",则需要将其PendingIntent.FLAG_UPDATE_CURRENT用作调用中的第4个参数getActivity().这仍将返回一个令牌,该令牌引用PendingIntent您创建的第一个令牌,但它也将替换该原件中的所有"额外",其中PendingIntent"附加" Intent作为第3个参数传递.
| 归档时间: |
|
| 查看次数: |
456 次 |
| 最近记录: |