我在Android應用程序中實現了Google Cloud Messaging。當我打開應用程序時使用JSON發送消息時,它的行爲與關閉應用程序時不同。當應用程序在後臺時推送通知
當我有應用程序打開,並收到通知,它開始我希望它啓動的意圖。當我收到通知時,我的應用程序已關閉,並且我單擊通知,它會打開主要目的。我怎麼能說我打開應用程序並收到推送通知時需要打開哪個意圖?
在MyGcmListenerService代碼
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
File fileDir, file;
String filename, filestring;
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
@Override
public void onMessageReceived(String from, Bundle data) {
Bundle notification = data.getBundle("notification");
String title = notification.getString("title");
String naam = notification.getString("naam");
String nfcId = notification.getString("nfcId");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Title: " + title);
Log.d(TAG, "Naam: " + naam);
Log.d(TAG, "nfcId: " + nfcId);
if (from.startsWith("/topics/")) {
} else {
filename = "gegevensOverledene";
ReadWriteFile readWriteFile = new ReadWriteFile(getApplicationContext());
readWriteFile.writeFileOverledene(filename, naam);
}
sendNotification(title, naam, nfcId);
}
/**
* Create and show a simple notification containing the received GCM message.
*/
private void sendNotification(String title, String naam, String nfcId) {
Intent intent = new Intent(this, PinLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("naam", naam);
intent.putExtra("nfcId",nfcId);
intent.putExtra("Class",NieuweOverledeneActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(naam)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
我送的JSON,請注意,我離開了token_mobile故意
{
"to": "TOKEN_MOBILE****************",
"notification": {
"title": "Nieuwe overledene",
"body": "Henk",
"naam": "Henk",
"nfcId": "80-40-A3-4A-C2-D6-04"
}
}
它是在你的sendNotification方法中,你需要設置正確的意圖到PendingIntent你開始 –
無論我在sendNotification中設置了哪個意圖,當我在應用程序關閉時收到消息時,它**總是* *啓動主要意圖 – willemjan92
這很奇怪 - 你可能在你的'PinLoginActivity'中做任何檢查,可能是將用戶重定向到主要活動?請向我們展示'PinLoginActivity'代碼。另外,嘗試將您的'意圖'標誌更改爲'intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);' – ishmaelMakitla