2016-10-13 15 views
1

PHPファイルから通知メッセージを送信し、Firebase Cloud Messaging(FCM)を使用して携帯電話に送信できました。Androidスタジオを使用したFCM通知

ユーザがモバイルトレイで通知メッセージをクリックすると、同じメッセージと時間と日付を持つ別のレイアウトにどのようにリダイレクトできますか?ここで

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{ 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    showNotification(remoteMessage.getData().get("message")); 
} 

private void showNotification(String message) { 

    Intent i = new Intent(this,MainActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle("Notification") 
      .setContentText(message) 
      .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
      .setContentIntent(pendingIntent) 
      .setSound(defaultSoundUri); 



    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    manager.notify(0,builder.build()); 
} 

}

答えて

1

//サービスファイル内

private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     Log.d("Send","msg" +messageBody); 
     Toast.makeText(getApplicationContext(),"msg" +messageBody ,Toast.LENGTH_SHORT); 
     intent.putExtra("msg",messageBody); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Firebase Push Notification") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 
    } 

//活動にFirebaseMessagingService.javaで自分のコード(アンドロイドStudioを使用して)され

Intent intent = getIntent(); 
       String text = intent.getStringExtra("msg"); 
       Log.d("Received","msg" +text); 
       Toast.makeText(getApplicationContext(),"Received" + text ,Toast.LENGTH_SHORT); 
+0

//アクティビティはMainActivity.javaに追加されますか? – Ksr

+0

はいそれを追加する必要があります。作成() –

+0

は、Intent、Log.d、Toastを解決できません。 &.getStringExtra ..?それはどういう意味? – Ksr

2

チェック彼の

private void sendNotification(String messageBody) 
{ 
Intent intent = new Intent(this, MainActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
     PendingIntent.FLAG_ONE_SHOT); 

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle("Firebase Push Notification") 
     .setContentText(messageBody) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(0, notificationBuilder.build()); 
} 
+0

PendingIntent.FLAG_UPDATE_CURRENTをPendingIntent.FLAG_ONE_SHOTに変更します)..? – Ksr

+0

はい...これで結果が得られます。 –

+0

動作していません...私はメールをurすることができます.. – Ksr

関連する問題