我在我的Android应用程序中实现了Google Cloud Messaging。当我在打开应用程序时使用JSON发送消息时,与关闭应用程序时的操作不同。
当我应用程序打开并收到通知时,它会启动我想要启动的意图。当我应用程序关闭并且接收到通知时,我单击通知时将打开主意图。如何确定在关闭我的应用程序并接收推送通知时需要打开哪个意图?
我发送的JSON,注意我故意省略了token_mobile。
当我应用程序打开并收到通知时,它会启动我想要启动的意图。当我应用程序关闭并且接收到通知时,我单击通知时将打开主意图。如何确定在关闭我的应用程序并接收推送通知时需要打开哪个意图?
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"
}
}
PinLoginActivity
中可能进行了任何检查,可能会将用户“重定向”到主活动吗?请向我们展示PinLoginActivity
代码。另外,请尝试将您的Intent
标志更改为intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
。 - ishmaelMakitlaPinLoginActivity
是我的主 Activity。在那里,我有一个方法来重定向到一个特定的类,我使用intent.putExtra("Class",NieuweOverledeneActivity.class);
和nextScreen = getIntent().getSerializableExtra("Class");
&Intent nextScreenIntent = new Intent(this,(Class) nextScreen);
。但是因为当应用程序关闭时sendNotification
没有被调用,它将没有下一个屏幕可序列化。 - willemjan92