【发布时间】:2019-12-20 11:00:52
【问题描述】:
我正在通过 c# 开发 API,它将向特定用户(android 用户)发送通知,然后当用户打开通知时我想将他重定向到特定活动。
所以我需要连同通知消息一起发送数据。我已经使用 Firebase 控制台对其进行了测试,它工作正常,收到通知并且我的启动器活动从已发送的数据中接收到额外的数据
我还从我的后端对其进行了测试,并收到了通知,只是我的启动器意图没有收到任何额外信息。
我已经挣扎了好几个小时了,任何想法都会有所帮助!
这是我的 c# 代码
public String getNotification ()
{
string serverKey = "xxxx";
var result = "-1";
try
{
var webAddr = "https://fcm.googleapis.com/fcm/send";
var regID = "xxxx";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"to\": \"" + regID +
"\",\"notification\": {\"title\": \"Testing\",\"body\": \"Hi Testing\"}" +
"," + "\"data:\"" + "{\"mymsg\":" + "\"h\" }}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return "Can't Send";
}
}
}
这是我的启动器活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("test" , "in main");
if (getIntent().getStringExtra("mymsg") != null) {
Log.d("test" , "has extra");
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
finish();
} else {
Log.d("test" , "no extra");
}
【问题讨论】:
-
@JakeB 我不能像这样将它存储在字符串值中
-
您能否在您的 Android 应用中第一次收到来自 firebase 的消息的位置发布代码。
标签: c# android api web-services push-notification