Mam app gdzie chcę zbudować przepływu 2 różni się w:GCM Jak wykryć, czy aplikacja jest otwarta, a jeśli tak, to pojawi się okienko alertu zamiast zwykłego przepływu powiadomień?
- 1.a App jest otwarty na wszelkie działania
1.b App pokaz AN alertbox gdzie użytkownik może wybrać, aby przejść do działalność związana z powiadomieniem lub pozostaniem na bieżącej działalności.
2.a App jest uruchomiony w tle
- 2.b powiadomienie w pasku powiadomień, rozpoczyna działalność w odniesieniu do zgłoszenia.
Obecnie przepływ 2 działa, ale nie mogę się dowiedzieć, jak uzyskać przepływ 1 działa. Oto niektóre kodu:
W GcmIntentService:
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM will be
* extended in the future with new message types, just ignore any message types you're
* not interested in, or that you don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.e("GCM", "Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.e("GCM", "Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification(extras);
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
String message = extras.getString("message");
Intent openIntent = new Intent(this, HomeActivity.class);
if (extras != null) {
if (extras.containsKey("tipid")) {
openIntent.putExtra("tipid", extras.getString("tipid"));
} else if (extras.containsKey("discussionid")) {
openIntent.putExtra("discussionid", extras.getString("discussionid"));
}
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
openIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("StadseBoeren")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message);
mBuilder.setContentIntent(contentIntent)
.setAutoCancel(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
GcmBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
HomeActivity onCreate:
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey("tipid")) {
pendingObjectId = extras.getString("tipid");
modelFlag = ModelFlag.TIP;
} else if (extras.containsKey("discussionid")) {
pendingObjectId = extras.getString("discussionid");
modelFlag = ModelFlag.DISCUSSION;
}
}
Myślę, że powinien być w stanie pracować, ale jestem Czy trzeba to wdrożyć we wszystkich działaniach, prawda? –
Nie ma potrzeby zaimplementowania tego we wszystkich działaniach, wystarczy zrobić to w klasie Application. Zobacz edytowaną sekcję. Za każdym razem, gdy połączenie zostanie odebrane, gdy czynność zostanie otwarta, zostanie zamknięte (wstrzymane). – shailesh
Pamiętaj, że jest to implementowane na poziomie ApI 14 – shailesh