我在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