[英]FCM not sending notification
I followed the fcm page however i can't get it to send a notification.我关注了 fcm 页面,但是我无法让它发送通知。 i tested the notification key parameter and it is not empty it is another device's Token.
我测试了通知键参数,它不为空,它是另一个设备的令牌。 this is my send notification function.
这是我的发送通知功能。
private void sendMessage() {
String sendMessageText = mSendEditText.getText().toString();
if(!sendMessageText.isEmpty()){
try {
FCMNotification.pushFCMNotification(notificationKey,myName,sendMessageText);
System.out.print("Entered try");
} catch (Exception e) {
System.out.print("Entered catch");
e.printStackTrace();
}
DatabaseReference newMessageDb = mDatabaseChat.push();
Map newMessage = new HashMap();
newMessage.put("userName", myName);
newMessage.put("createdByUser", currentUserId);
newMessage.put("text", sendMessageText);
mScrollView.postDelayed(new Runnable() {
@Override
public void run() {
//replace this line to scroll up or down
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 100L);
newMessageDb.setValue(newMessage);
FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId).child("Unread").child(mMatchId).setValue(true);
}
mSendEditText.setText(null);
mSendButton.clearFocus();
mSendEditText.requestFocus();
}
This is what worked for me:这对我有用:
public class FCMNotification {
public final static String AUTH_KEY_FCM = "your_key";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(String DeviceIdKey) throws Exception {
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + authKey);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject data = new JSONObject();
data.put("to", DeviceIdKey.trim());
JSONObject info = new JSONObject();
info.put("title", "FCM Notificatoin Title"); // Notification title
info.put("text", "Hello First Test notification"); // Notification body
data.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
FCMNotification.pushFCMNotification("token_of_the_device");
}
}
Also they made some changes in receiving the message:他们还对接收消息进行了一些更改:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
/**
* Service that is responsible for receiving firebase messages and handling
them
*
* @author filip.trajkovski
* @version 1.0
* @since 1.0
*/
public class FirebaseListenerService extends FirebaseMessagingService {
public static final String TAG = FirebaseListenerService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "Message received from: " + remoteMessage.getFrom());
Log.d(TAG,"----- THIS IS THE MESSAGE RECEIVED ------");
Log.d(TAG, "Message: " + remoteMessage.getNotification().getBody());
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
}
} }
And add this in your AndroidManifest.xml:并将其添加到您的 AndroidManifest.xml 中:
<service android:name=".cloud.msg.FirebaseListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.