2016-11-19 18 views
5

Android:如何在Android Manifest文件中動態地將通知操作綁定到活動?如何將通知點按操作應用於Android中的特定活動?

專門爲消防基地整合。 請給點建議。謝謝

+0

什麼是你的使用情況? –

+0

從api我得到不同的情況通知意圖與不同的活動。當應用程序打開工作正常,但在後臺應用程序它不工作 – Pradip

+0

如果我正確理解問題,您將不得不添加意向過濾器到所有這些活動,需要在通知到達時打開。您可以在通知有效內容中包含活動網址,並在Receiver中使用它來啓動該操作的意圖。 –

回答

2

爲了實現打開活動的功能,在收到通知後,應該在清單中給出該活動intent-filter以偵聽一個動作。

在Notification和有效負載中,應該發送活動偵聽的動作。在Receiver中,可以觸發帶有這種操作的Intent,這會打開活動,即使應用程序處於背景或關閉狀態。

4

Recive自定義消息在通知並根據KeyWork真或假的或指定的Word中,進入到活動 公共類MyFirebaseMessagingService擴展FirebaseMessagingService {

private static final String TAG = "FirebaseMessageService"; 
Bitmap bitmap; 

/** 
* Called when message is received. 
* 
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
*/ 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // There are two types of messages data messages and notification messages. Data messages are handled 
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
    // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
    // When the user taps on the notification they are returned to the app. Messages containing both notification 
    // and data payloads are treated as notification messages. The Firebase console always sends notification 
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options 
    // 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 

    //The message which i send will have keys named [message, image, AnotherActivity] and corresponding values. 
    //You can change as per the requirement. 

    //message will contain the Push Message 
    String message = remoteMessage.getData().get("message"); 
    //imageUri will contain URL of the image to be displayed with Notification 
    String imageUri = remoteMessage.getData().get("image"); 
    //If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened. 
    //If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity will be opened. 
    String TrueOrFlase = remoteMessage.getData().get("AnotherActivity"); 

    //To get a Bitmap image from the URL received 
    bitmap = getBitmapfromUrl(imageUri); 

    sendNotification(message, bitmap, TrueOrFlase); 

} 


/** 
* Create and show a simple notification containing the received FCM message. 
*/ 

private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("AnotherActivity", TrueOrFalse); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setLargeIcon(image)/*Notification icon image*/ 
      .setSmallIcon(R.drawable.firebase_icon) 
      .setContentTitle(messageBody) 
      .setStyle(new NotificationCompat.BigPictureStyle() 
        .bigPicture(image))/*Notification with Image*/ 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

This is AnotherActivity 

//如果通知消息被竊聽,任何數據伴隨着通知 //消息在意向演員中可用。在這個項目中,啓動器 //意圖在通知被點擊時觸發,所以任何伴隨的數據將在這裏處理 。如果您希望激發不同的意圖,請將通知消息的click_action //字段設置爲所需的意圖。啓動器意圖 //在沒有指定click_action時使用。 // //處理伴隨通知消息的可能數據。 如果(getIntent()。getExtras()!= NULL){

 for (String key : getIntent().getExtras().keySet()) { 
      String value = getIntent().getExtras().getString(key); 

      if (key.equals("AnotherActivity") && value.equals("True")) { 
       Intent intent = new Intent(this, AnotherActivity.class); 
       intent.putExtra("value", value); 
       startActivity(intent); 
       finish(); 
      } 

     } 
    } 

這是數據

{ "data": { 
    "image": "https://ibin.co/2t1lLdpfS06F.png", 
    "message": "Firebase Push Message Using API" 
    "AnotherActivity": "True" 
    }, 
    "to" : "f25gYF3***********************HLI" 
} 
相關問題