2017-08-07 5 views
-3

public class MyFirebaseMessagingServiceはFirebaseMessagingServiceを拡張します{ private static final String TAG = "MyFirebaseMsgService";firebaseのクラウドメッセージングから通知を受け取ることはできますが、どのようにしてユーザインタフェース上に表示できますか?

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.d(TAG,"FROM:"+remoteMessage.getFrom()); 

    //check if the message contains data 
    if(remoteMessage.getData().size()>0){ 
     Log.d(TAG,"Message data:"+remoteMessage.getData()); 

     //check if the message contains notification 
     if(remoteMessage.getNotification()!=null){ 
      Log.d(TAG,"Message body:"+remoteMessage.getNotification().getBody()); 
      sendNotification(remoteMessage.getNotification().getBody()); 

     } 
    } 
} 

ます。private void sendNotificationを(文字列本体){

Intent intent=new Intent(this,MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT); 
    //set sound notification 
    Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Firebase Cloud Messaging") 
      .setContentText(body) 
      .setAutoCancel(true) 
      .setSound(notifictaionSound) 
      .setContentIntent(pendingIntent); 
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0/*ID of notification*/,notifiBuilder.build()); 

パブリッククラスMainActivityがAppCompatActivityを拡張{

private static final String TAG = "MainActivity"; 
TextView text = (TextView) findViewById(R.id.textView); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Intent intent=getIntent(); 

    if(intent !=null){ 
     String message=intent.getStringExtra("message"); 
     text.setText(message); 

    } 
    Button btnShowToken=(Button) findViewById(R.id.button_show_token); 
    btnShowToken.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String token= FirebaseInstanceId.getInstance().getToken(); 
      Log.d(TAG, "Token:"+token); 
      Toast.makeText(MainActivity.this,token, Toast.LENGTH_SHORT).show(); 
     } 

    }); 
} 

}

+0

通知を作成しますか?あなたが望むものをより良く説明してください –

+0

私は通知を受け取った後、通知メッセージをUIに表示することを意味します –

+0

投稿 'sendNotification()'コード –

答えて

0

だからあなたはこれを試すことができます。

Intent intent=new Intent(this,MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("message",body); 
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT); 
    //set sound notification 
    Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Firebase Cloud Messaging") 
      .setContentText(body) 
      .setAutoCancel(true) 
      .setSound(notifictaionSound) 
      .setContentIntent(pendingIntent); 
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0/*ID of notification*/,notifiBuilder.build()); 

そしてMainActivity onCreate()メソッドの中には、次の操作を行います。

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

if(message !=null){ 
    //Create ui 
} 

だから私は、あなたがしたいUIを知りませんが、これはあなたが唯一のアクティビティ内のUIを作成する必要があり、通知からメッセージを取得します。

説明

これはあなたのケース本体には、追加の文字列と意図を作成し、通知のpendingIntentとして意図設定します。 ユーザーが通知をクリックすると、アプリが開いて、通知をクリックしてユーザーを起動したかどうかを確認します。はいの場合は、意図した余分な情報を取得し、何もしない場合はuiを作成します。

+0

あなたの助言をありがとうが、私のアプリは変更後に開くことができません。 –

+0

私はすでにUIコードを追加しました –

+0

[TextView text =(TextView)findViewById(R.id.textView);]をOncreate()に追加しました。私のアプリは動作していますが、通知のメッセージはUIに表示されません –

関連する問題