如果我们获得参数1,我发送带有不同参数1,2的不同方法的通知然后如果我们从通知管理器读取通知它应该重定向到chatActvity,如果我们接收参数2那么它应该重定向到Home活动当我阅读通知通知管理员请帮我下面哪里做错了以下是我的代码:
当我从服务器发送消息时,我收到消息和所有参数:我无法重定向到主页活动和聊天活动
public class GcmIntentService extends IntentService {
Context context;
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
String msg;
String senderID;
String recieverID;
int value = 1;
int reccvingparamtere;
String time;
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
msg = intent.getStringExtra("message");
senderID = intent.getStringExtra("sender_id");
recieverID = intent.getStringExtra("reccvier_id");
reccvingparamtere = intent.getIntExtra("reccvingparamtere",
reccvingparamtere);
time = intent.getStringExtra("time");
// test=intent.getStringExtra("_test");
SharedPreferences prefs = GcmIntentService.this.getSharedPreferences(
AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
Chat chat = new Chat();
chat.setMessage(msg);
chat.setSenderID(senderID);
chat.setRecieverID(prefs.getString(AppConstants.MEMBER_ID, ""));
chat.setSenderName("");
chat.setRecieverName("");
DBHelper dbHelper = new DBHelper(GcmIntentService.this);
dbHelper.addMessage(chat);
AppConstants.RECEIVED_MSG = msg;
AppConstants.RECEIVED_RCVID = senderID;
AppConstants.RECEIVED_SENDID = recieverID;
AppConstants.RECEIVED_TIME = new Date().toString();
GcmIntentService.this.sendBroadcast(intent);
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
if (reccvingparamtere == 1) {
sendNotification("Send error: " + extras.toString(),
senderID);
} else if (reccvingparamtere == 2) {
sendNotification2("Send error: " + extras.toString(),
senderID);
}
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification(
"Deleted messages on server: " + extras.toString(),
senderID);
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++) {
Log.i(TAG,
"Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
// sendNotification("Received: " + extras.toString());
if (LociiApplication.FID.equals(senderID))
System.out.println("Dont show notifications!");
else
sendNotification(msg, senderID);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg, String sender_id) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, ChatActivity.class);
myintent.putExtra("message", msg);
myintent.putExtra("to_id", sender_id);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.lokii_notification_icon)
.setContentTitle("Locii Messge").setAutoCancel(true)
// .setSound(Uri.parse("android.resource://package_name/raw/sound"))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private void sendNotification2(String msg, String sender_id) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, HomeActivity.class);
myintent.putExtra("message", msg);
myintent.putExtra("to_id", sender_id);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.lokii_notification_icon)
.setContentTitle("Locii Messge").setAutoCancel(true)
// .setSound(Uri.parse("android.resource://package_name/raw/sound"))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
答案 0 :(得分:0)
我不确定这是什么问题,但我可以向你提出一些观点。
Intent
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
2。使用TaskStackBuilder
创建您的PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(SomeActivity.class);
Intent myintent = new Intent(this, HomeActivity.class);
myintent.putExtra("message", msg);
myintent.putExtra("to_id", sender_id);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
stackBuilder.addNextIntent(myintent);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
希望有所帮助