Tôi có triển khai thông báo GCM. Tôi biết rằng ứng dụng máy khách nhận được thông báo cho dù nó đang ở nền trước, nền hay trạng thái bị giết. Những gì tôi muốn biết là, làm thế nào tôi có thể khởi chạy ứng dụng của tôi trên thông báo nhận được, khi ứng dụng ở trạng thái bị giết?Khởi chạy ứng dụng khi thông báo GCM nhận được
Trả lời
Trong nhận tin nhắn, tôi thực hiện như sau:
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Đây thay YourActivity.class với các hoạt động nhập cảnh. Điều này làm việc cho tôi.
bạn có thể sử dụng NotificationManager để bắt đầu hoạt động của mình.
cố gắng sử dụng Dưới đây Inside onMessage() phương pháp của bạn Đó là phương pháp ghi đè trong lớp mà kéo dài GCMBaseIntentService lớp của GCM.
int icon = R.drawable.your_app_icon;
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, YOUR_ACTIVITY.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// notificationIntent.putExtra("PostName", vPostText);
// Log.i(TAG, "Sent Postid " + postid);
// Util util = (Util) context.getApplicationContext();
// util.setPostID(postid);
// util.setNotify(true);
// util.setUserNAME(vPortCode);
// util.setPostNAME(vPostText);
// util.setmEDIA(vMedia);
// util.setmEDIATHHUMB(vMediaThumb);
// util.setmEDIATYPE(vMediaType);
// util.setAirportName(vAirportName);
notificationIntent.putExtra("Set_image", true);
notificationIntent.putExtra("Notify", true);
// set intent so it does not start a new activity
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context,
(int) System.nanoTime(), notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify((int) System.nanoTime(), notification);
Cảm ơn bạn.Tôi biết cách bắt đầu một hoạt động từ ứng dụng. Nhưng, điều tôi muốn làm là, khi nhận được thông báo, nếu ứng dụng của tôi ở chế độ nền, tôi sẽ mang nó đến mặt đất, nếu nó ở nền trước thì tôi sẽ tiếp tục xử lý và nếu ứng dụng của tôi ở trạng thái dừng/bị giết sau đó tôi muốn "khởi chạy lại ứng dụng" không hoạt động. Vì vậy, bạn có thể cho tôi biết nếu có bất kỳ cách nào để khởi chạy ứng dụng khi ứng dụng ở trạng thái dừng/bị giết trên thông báo nhận được không? – jasdmystery
@jasdmystery: bạn có tìm ra được không? Tôi đang đối mặt với cùng một vấn đề. – user836026
Tham khảo câu trả lời ở trên. – jasdmystery
Điều gì sẽ xảy ra nếu ứng dụng đang chạy? – Ahmed
Điều gì sẽ xảy ra nếu ứng dụng bị xóa khỏi danh sách công việc? trong trường hợp đó "người nhận tin nhắn" sẽ không được gọi. Nó sẽ xử lý bởi hệ thống và nó được nhấp bằng thẻ "click_action". Nhưng mối quan tâm của tôi là mở hoạt động mục tiêu của tôi sau khi thông báo đến. –
làm thế nào để có được bối cảnh ở đây trong tin nhắn fcm ?? –