如何在没有点击通知的情况下显示活动

我想要在通知发出后显示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 

回答:

请在你的代码延伸AppComatActivity和所有其他活动延伸BaseActivity基地活动,在节目通知内广播你的意图与新的活动,并在基地活动接收器,在该接收器内,你可以打开你的活动

更多象我可以显示如何为这个代码,

感谢

回答:

试试这个, 可能是有帮助的你,

添加在onMessageReceived()这段代码方法,

在NotificationActivity.class

if (getIntent().getBooleanExtra("isNotification", false)) { 

message = getIntent().getStringExtra("message");

txt_message.setText(message);

}

我简单的GET消息3210

添加此,

@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

}

以上是 如何在没有点击通知的情况下显示活动 的全部内容, 来源链接: utcz.com/qa/262918.html

回到顶部