我希望在收到通知后显示activity
而不点击通知Activity
必须打开。我正在使用FCM
进行通知发送。
如何在不点击通知的情况下打开Activity
?
我正在使用它,但它没有打开活动:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
String messageContent = "";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
messageContent = remoteMessage.getData().get("message");
showNotification(remoteMessage.getData().get("message"));
postToastMessage(remoteMessage.getData().get("message"));
Log.d(TAG, "From: " + remoteMessage.getFrom());
}
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message +" received", Toast.LENGTH_LONG).show();
Intent newIntent = new Intent(MyFirebaseMessagingService.this, CallActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
}
});
}
private void showNotification(String message) {
Intent i = new Intent(this, CallActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(" Test")
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
我收到此错误
FA: Discarding data. Failed to send event to service
答案 0 :(得分:0)
试试这个, 可能对你有帮助,
在onMessageReceived()方法中添加此代码,
Intent intent = new Intent(MyFirebaseMessagingService.this, NotificationActivity.class);//here put your activity name which you want to open
intent.putExtra("isNotification", true);
intent.putExtra("message", message);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
我在NotificationActivity.class中简单地获取消息
if (getIntent().getBooleanExtra("isNotification", false)) {
message = getIntent().getStringExtra("message");
txt_message.setText(message);
}
添加此内容,
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "onMessage receiveddd: " + remoteMessage.getData());
// Log.e(TAG, "onMessage notification: " + remoteMessage.getNotification().getBody());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e("notification background", String.valueOf(remoteMessage.getData()));
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
String deviceID = data.get("DeviceID");
String message = data.get("Message");
Log.e("value11111111", deviceID);
Log.e("value222222222", message);
Intent intent = new Intent(MyFirebaseMessagingService.this, NotificationActivity.class);
intent.putExtra("isNotification", true);
// intent.putExtra("title", value1);
intent.putExtra("message", message);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// sendNotification("message received");
}
}
比邮递员, 发送此请求
{
"data":{
"DeviceID":"",// pass here your DeviceID
"Message":"hiiii" // pass here your message
},
"to":""// pass here ypur tocken
}
答案 1 :(得分:-1)
在您的代码中创建一个BaseActivity,扩展AppComatActivity,所有其他活动扩展基本活动,在show通知中通过新活动广播您的意图并使接收者进行基本活动,在该接收者中您可以打开您的活动
对于更多的构造,我可以展示如何编码,
由于