I manage to send notification using Firebase Cloud Messenging (FCM) to android phone. The problem is that, when I click the notification on the status bar / notification area to open to new activity, the notification message text in the status bar is not carried foward to my new activity page. How to solve this problem. I want the text message "Success! This message is from PHP" can be seen in my new activity page (activiy_main.xml).
我设法使用Firebase Cloud Messenging(FCM)向Android手机发送通知。问题是,当我单击状态栏/通知区域上的通知以打开新活动时,状态栏中的通知消息文本不会转到我的新活动页面。如何解决这个问题呢。我想在我的新活动页面(activiy_main.xml)中看到短信“成功!此消息来自PHP”。
//example of php site
// php网站的例子
<?php
echo "Success! This message is from PHP";
?>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fcm.notification.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#000000"
android:text="Hello World!" />
</RelativeLayout>
1 个解决方案
#1
0
Your receiver class should look like this:
您的接收器类应如下所示:
public class MyGcmPushReceiver extends GcmListenerService {
private static final String TAG = MyGcmPushReceiver.class.getSimpleName();
private NotificationUtils notificationUtils;
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param bundle Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
@Override
public void onMessageReceived(String from, Bundle bundle) {
String title = bundle.getString("title");
String message = bundle.getString("message");
String image = bundle.getString("image");
String timestamp = bundle.getString("created_at");
Log.e(TAG, "From: " + from);
Log.e(TAG, "Title: " + title);
Log.e(TAG, "message: " + message);
Log.e(TAG, "image: " + image);
Log.e(TAG, "timestamp: " + timestamp);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils();
notificationUtils.playNotificationSound();
} else {
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
//Here you set the message as an extra to the intent.
resultIntent.putExtra("message", message);
if (TextUtils.isEmpty(image)) {
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
} else {
showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, image);
}
}
}
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
/**
* Showing notification with text and image
*/
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
}
#1
0
Your receiver class should look like this:
您的接收器类应如下所示:
public class MyGcmPushReceiver extends GcmListenerService {
private static final String TAG = MyGcmPushReceiver.class.getSimpleName();
private NotificationUtils notificationUtils;
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param bundle Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
@Override
public void onMessageReceived(String from, Bundle bundle) {
String title = bundle.getString("title");
String message = bundle.getString("message");
String image = bundle.getString("image");
String timestamp = bundle.getString("created_at");
Log.e(TAG, "From: " + from);
Log.e(TAG, "Title: " + title);
Log.e(TAG, "message: " + message);
Log.e(TAG, "image: " + image);
Log.e(TAG, "timestamp: " + timestamp);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils();
notificationUtils.playNotificationSound();
} else {
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
//Here you set the message as an extra to the intent.
resultIntent.putExtra("message", message);
if (TextUtils.isEmpty(image)) {
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
} else {
showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, image);
}
}
}
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
/**
* Showing notification with text and image
*/
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
}