嗨朋友,我过去几天疯了,我不知道为什么会这样,请帮忙。
我在我的应用中使用firebase推送通知,从后端到我的应用程序。问题是当应用程序打开时单击推送通知时,它会重定向到正确的类。但是当app关闭时推送通知出现时,它只会进入主类。
FireBase.class
public class MyFirebaseMessagingService extends FirebaseMessagingService {
Intent intent;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
Constant.l(remoteMessage.getNotification().getBody());
}
if (remoteMessage.getData().containsKey("mtype")) {
Constant.l(String.valueOf(remoteMessage.getData()));
sendNotification(getApplicationContext(), remoteMessage.getData().get("message"), remoteMessage.getData().get("id"), remoteMessage.getData().get("type"), remoteMessage.getData().get("username"), remoteMessage.getData().get("mtype"));
}
}
private void sendNotification(Context context, String messageBody, String sid, String type, String username, String mtype) {
if (Session.isLoggedIn(getApplicationContext())) {
switch (mtype) {
case "single":
Intent chathis = new Intent(ChatHistory.BroadCastAction);
chathis.putExtra("msg", messageBody);
chathis.putExtra("type", type);
context.sendBroadcast(chathis);
intent = new Intent(this, ChatHistory.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("mtype", "single");
intent.putExtra("oppusername", username);
intent.putExtra("oid", sid);
break;
case "group":
Intent grphis = new Intent(Group_History.BroadCastAction);
grphis.putExtra("msg", messageBody);
grphis.putExtra("sid", sid);
grphis.putExtra("type", type);
grphis.putExtra("usern", username);
context.sendBroadcast(grphis);
intent = new Intent(context, Group_History.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("mtype", "single");
intent.putExtra("oppusername", username);
intent.putExtra("oid", sid);
break;
default:
intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
}
}
PendingIntent pendingIntent = null;
try {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
} catch (Exception e) {
Constant.l(e.toString());
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(username.replace("$", ""))
.setContentText(messageBody.replace("$", " "))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
的config.php
class PushNotifications {
//Firebase
private static $apiKey = 'AAAACmtNrq0:APA91bHFH6KKA3irMMlc99hoJhTASuluM_KyNtyUYmcnWK41QeWxI9EystYFNBRwSNOqLtAB24xZ_bh_0YLugEkFfb94VYmAiJHWOlQgmlQP-4jMQnClE1_mCgnItOgnI2EAnNmQadcrko-oS-iU1eCwNPrsxPRXlQ';
// Change the above three vriables as per your app.
public function __construct() {
//exit('Init function is not allowed');
}
// Sends Push notification for Android users
public function send($data, $reg_id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$message = array(
"message" => $data['mdesc'],
"id" => $data['facebook_id'],
"username" => $data['username'],
"type" => $data['type'],
"mtype" => $data['mtype'],
"profile" =>$data['profile']
);
$headers = array(
'Content-Type:application/json',
'Authorization:key='.self::$apiKey,
);
$fields = array(
'notification' => array("title" => $data['mtitle'], "body" => $data['mdesc'], "sound"=>"default",
"icon"=>"fcm_push_icon"),
'data' => $message,
'to' => $reg_id,
"priority" => "high",
"restricted_package_name"=>""
);
return $this->useCurl($url, $headers, json_encode($fields));
}
// Curl
private function useCurl($url, $headers, $fields = null) {
// Open connection
$ch = curl_init();
if ($url) {
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($fields) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
return $result;
}
}
}
$push = new PushNotifications();
答案 0 :(得分:0)
在Android中,当应用关闭时,点击通知后,它始终会重定向到启动器活动。
当应用关闭时,如果您收到来自fcm的通知,则必须在启动器活动中处理通知。将意图从启动器转移到列出所有通知的所需类别。
答案 1 :(得分:0)
我已通过删除此行'notification'=>解决了这个问题。 array(“title”=> $ data ['mtitle'],“body”=> $ data ['mdesc'],“sound”=>“默认”, 来自config.php的“icon”=>“fcm_push_icon”)