2016-10-22 14 views
0

Firebase Cloud Messenging(FCM)を使用してAndroid端末に通知を送信できます。問題は、ステータスバー/通知領域の通知をクリックして新しいアクティビティを開くと、ステータスバーの通知メッセージテキストが新しいアクティビティページに移動しないということです。この問題を解決する方法。私の新しい活動ページ(activiy_main.xml)にテキストメッセージ "成功!このメッセージはPHPから来ました"が表示されます。FCM(Androidスタジオ)を使用してPHPからAndroid端末への通知をプッシュ

// PHPのサイトあなたの受信機のクラスは、次のようになります

<?php 
    echo "Success! This message is from PHP"; 
?> 

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="fcm.notification.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="30dp" 
     android:textColor="#000000" 
     android:text="Hello World!" /> 
</RelativeLayout> 

答えて

0

の例:

public class MyGcmPushReceiver extends GcmListenerService { 

    private static final String TAG = MyGcmPushReceiver.class.getSimpleName(); 

    private NotificationUtils notificationUtils; 

    /** 
    * Called when message is received. 
    * 
    * @param from SenderID of the sender. 
    * @param bundle Data bundle containing message data as key/value pairs. 
    *    For Set of keys use data.keySet(). 
    */ 

    @Override 
    public void onMessageReceived(String from, Bundle bundle) { 
     String title = bundle.getString("title"); 
     String message = bundle.getString("message"); 
     String image = bundle.getString("image"); 
     String timestamp = bundle.getString("created_at"); 
     Log.e(TAG, "From: " + from); 
     Log.e(TAG, "Title: " + title); 
     Log.e(TAG, "message: " + message); 
     Log.e(TAG, "image: " + image); 
     Log.e(TAG, "timestamp: " + timestamp); 

     if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 

      // app is in foreground, broadcast the push message 
      Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
      pushNotification.putExtra("message", message); 
      LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

      // play notification sound 
      NotificationUtils notificationUtils = new NotificationUtils(); 
      notificationUtils.playNotificationSound(); 
     } else { 

      Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); 
      //Here you set the message as an extra to the intent. 
      resultIntent.putExtra("message", message); 

      if (TextUtils.isEmpty(image)) { 
       showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent); 
      } else { 
       showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, image); 
      } 
     } 
    } 

    /** 
    * Showing notification with text only 
    */ 
    private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { 
     notificationUtils = new NotificationUtils(context); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     notificationUtils.showNotificationMessage(title, message, timeStamp, intent); 
    } 

    /** 
    * Showing notification with text and image 
    */ 
    private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { 
     notificationUtils = new NotificationUtils(context); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl); 
    } 
} 
関連する問題