我正在向用户发送使用云功能的通知。当用户点击通知时,它应该重定向到Intent页面并显示数据消息。当应用程序处于后台时,我遇到了问题
MyFirebaseMesssaging服务类
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONObject;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MY android fcm service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String FirstName = "0";
String Amount = "0";
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
FirstName = remoteMessage.getData().get("Name");
Amount = remoteMessage.getData().get("Amount");
Log.d(TAG, "From:" + remoteMessage.getFrom());
Log.d(TAG, "Notification MessageBody:" + remoteMessage.getNotification().getBody());
createNotification(remoteMessage.getNotification().getBody(),FirstName,Amount);
}
}
private void createNotification(String messageBody, String FirstName, String amount) {
Intent intent = new Intent( this,ReceivedNotification.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("FirstName",FirstName);
intent.putExtra("Amount",amount);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("deyaPay")
.setContentText(messageBody)
.setAutoCancel(true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent)
.setVisibility(Notification.VISIBILITY_PUBLIC);
//.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
//.addAction(R.drawable.accept, "Accept",) // #0
//.addAction(R.drawable.ic_reject, "Reject", ResultActivity.class) ; // ;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
ReceivedNotification类(意图页面)
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
public class ReceivedNotification extends AppCompatActivity {
private TextView Name;
private TextView amount;
public static String TAG = "REceivved";
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle("Received notifications");
super.onCreate(savedInstanceState);
setContentView(R.layout.receivednotification);
Name = (TextView)findViewById(R.id.textView12);
amount =(TextView)findViewById(R.id.textView14);
Intent i = getIntent();
if (i != null && i.getExtras() != null) {
Bundle extras = i.getExtras();
String FirstName = extras.getString("FirstName");
String amount1 = extras.getString("Amount");
Name.setText(FirstName);
amount.setText(amount1);
}
}
}
}
用于发送通知的云功能
const functions = require('firebase-functions');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.splitting = functions.firestore
.document('deyaPayUsers/{authid}/Split/{authid1}/SentInvitations/{autoid}')
.onWrite(event =>{
var id = event.params.authid;
var dbref = db.collection('deyaPayUsers').doc(id);
var na = dbref.FirstName;
console.log(na);
console.log(id);
var id1 = event.params.authid1;
var splitid = event.params.autoid;
var document = event.data.data();
var phoneNumber = [];
for(var k in document){
phoneNumber.push(document[k].PhoneNumber);
}
console.log("The json data is " +phoneNumber);
var ph1 = document.Invite1.PhoneNumber;
var amount = document.Invite1.Amount;
var a = amount.toString();
console.log(a);
console.log(document);
console.log(ph1);
var deyaPay = db.collection("deyaPayUsers");
let phQuery = deyaPay.where('PhoneNumber','==',ph1)
.get()
.then(snapshot => {
snapshot.forEach(doc=>{
console.log(doc.id, " => ", doc.data());
var userData = doc.data();
var userId = doc.id;
var FirstName = userData.FirstName;
console.log(FirstName);
var LastName = userData.LastName;
console.log(FirstName);
var FullName = FirstName + LastName;
console.log(FullName);
var Token = userData.FCMToken;
console.log(userId);
console.log("FCM Token for that phoneNumber" + Token);
console.log(userData.PhoneNumber);
console.log(ph1 + "Exist In DB");
//这是用于发送通知消息
let payload = {
notification: {
title: "Message",
body: 'request to pay ${(FirstName)}',
sound: "default",
},
'data':{
'Name':FirstName,
'Amount':a
}
};
console.log(payload);
return admin.messaging().sendToDevice(Token, payload).then((response)=> { // This method is used for returning the notification to a specific device
console.log(Token);
console.info("Successfully sent notification")
}).catch(function(error) {
console.warn("Error sending notification " , error)
});
});
}) .catch(err => {
console.log('Error getting documents', err);
});
});
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bodaty.deyaPay.deyaPay">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name="com.bodaty.deyaPay.deyaPay.GlobalAmount"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="deyaPay"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.bodaty.deyaPay.deyaPay.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.bodaty.deyaPay.deyaPay.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.bodaty.deyaPay.deyaPay.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
<activity android:name="com.bodaty.deyaPay.deyaPay.Signup" />
<activity android:name="com.bodaty.deyaPay.deyaPay.ResetPasswordActivity" />
<activity android:name="com.bodaty.deyaPay.deyaPay.EmailVerification" />
<activity android:name="com.bodaty.deyaPay.deyaPay.AddMoney"/>
<activity android:name=".deyaPay"/>
<activity android:name=".PaymentMethods"/>
<activity android:name=".Stripe"/>
<activity android:name=".ACH"/>
<activity android:name=".ViewProfile"/>
<activity android:name=".EditProfile"/>
<activity android:name=".Split"/>
<activity android:name=".Display"/>
<activity android:name=".MessageReceived" />
<activity android:name=".SentNotificationReceivedNotification"/>
<activity android:name=".ReceivedNotification"/>
<activity android:name=".SentNotification"/>
</application>
</manifest>
答案 0 :(得分:-1)
当应用程序处于后台时,如果是混合或通知,android系统会处理通知。因此,当用户单击点击通知时,意图将传递给Laucnher活动。您可以通过
获取数据import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
public class ReceivedNotification extends AppCompatActivity {
private TextView Name;
private TextView amount;
public static String TAG = "REceivved";
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle("Received notifications");
super.onCreate(savedInstanceState);
setContentView(R.layout.receivednotification);
Name = (TextView)findViewById(R.id.textView12);
amount =(TextView)findViewById(R.id.textView14);
Intent i = getIntent();
if(getIntent.hasExtra("Name")){
Name.setText(getIntent.getStringExtra("Name"));
amount.setText(getIntent.getStringExtra("Amount"));
} else {
(i != null && i.getExtras() != null) {
Bundle extras = i.getExtras();
String FirstName = extras.getString("FirstName");
String amount1 = extras.getString("Amount");
Name.setText(FirstName);
amount.setText(amount1);
}
}
}
}
更改MainActivity.class
public class MainActivity extends AppCompatActivity {
private TextView Name;
private TextView amount;
public static String TAG = "REceivved";
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle("Received notifications");
super.onCreate(savedInstanceState);
setContentView(R.layout.receivednotification);
if(getIntent.hasExtra("Name")){
Name.setText(getIntent.getStringExtra("Name"));
amount.setText(getIntent.getStringExtra("Amount"));
sendNotificationIntent(getIntent.getStringExtra("Name"), getIntent.getStringExtra("Amount"));
}
}
private void sendNotificationIntent(String name, String amount){
Intent intent = new Intent(this, ReceivedNotification.class);
intent.putExtra("Name", name);
intent.putExtra("Amount", amount);
startActivity(intent);
finish();
}
}