일부 데이터는 GCMIntentService 인 텐트 클래스의 onMessage 리스너에있는 SharedPreferences에 저장할 수 있습니다. 결국 GCM 수신기는 귀하의 패키지 앱에 속합니다. 저장 내용은 앱과 메시지 페이로드에 따라 다르지만 원하는대로 설정할 수 있습니다. 그런 다음 알림을 클릭하면 활동의 onCreate 기능이 실행되면 공유 환경 설정을 읽고 GCM 알림을 받았는지 여부를 확인합니다. SharedPreferences에 저장 한 변수를 지우는 것이 좋습니다. 그러면 사용자가 다음에 앱을 열 때 내용이 제대로 표시됩니다.
여기에 예제가 있습니다. 불행히도 지금은 시도 할 수 없지만 아이디어를 보는 것이 유용합니다. G2DM과 매우 유사하므로 동등한 제품을 찾아야합니다.
public class GCMIntentService extends GCMBaseIntentService {
/*... other functions of the class */
/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("your_message");
// notifies user
generateNotification(context, message);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
// Save your data in the shared preferences
SharedPreferences prefs = getSharedPreferences("YourPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putBoolean("comesFromGCMNotification", true);
prefEditor.commit();
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
응답 해 주셔서 감사합니다. :-) 이것은 우리가 이야기하고있는 것에 대해 sharedprefs를 사용하지 않고서 이미 내가하고있는 것에 매우 흡사합니다. 내 머리를 감싸는 데 문제가있는 것은 "comeFromGCMNotification"부분이며 사용자가 알림에서 시작했는지 여부를 정확히 알 수 있습니다. 사용자가 알림을 받고 "comeFromGCMNotification"이 설정되면 알림을 선택하지 않고 앱을 시작하면 "cameFromGCMNotification"은 여전히 true이고 처리가 완료 될 때까지 계속됩니다. 내 추론에 내가 틀렸어? – MrTristan
@MrTristan 두 작업 공간 (Activity 및 GCMIntentService)에서 동일한 기본 설정을 사용하고 있는지 확인하십시오. 나의 예에서는 "YourPrefs"라고 부릅니다. 존재하지 않는 환경 설정을 요구할 때 기본 반환 값을 확인할 수도 있습니다. 이를 테스트하려면 "comesFromGCMNotification"에 대한 부울 대신 문자열을 사용하고 결과가 기본값 (빈 문자열 또는 null 일 가능성이 있음) 대신 실제로 입력 한 값인지 확인하십시오. – Esparver
내 의견을 잘못 말한 것 같습니다. comeFromGCMNotification의 가치를 읽는 것은 문제가 아닙니다 ... 그것은 개념적 수준에서 흐름이 잘못 될 것이라고 생각합니다. 나는 당신이 알림을받은 후 그것을 받으면 그 환경 설정을 설정, 그것은 알림을 선택하지 않고 응용 프로그램을 시작할 때 여전히 사실이 될 것 같아요 ... 나는 당신이 시작 여부에 따라 다른 경험을 가질 수 있기를 바랍니다. 알림을 선택하거나 홈 화면을 통해 앱을 실행합니다. – MrTristan