1

投稿がアップロードされたときに通知をアプリケーションのエンドユーザーにプッシュしようとしています。アプリがフォアグラウンドとバックグラウンドにあるときはうまく動作しますが、アプリが強制終了または終了したときに表示されません(バックグラウンドとフォアグラウンドではありません)。アプリが終了したときに通知を表示する方法はありますか? firebase関数にデプロイしたindex.jsコードを示します。ここ firebaseを使用しているアプリケーションの終了状態で通知が表示されない

const functions = require('firebase-functions'); 
let admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 
exports.sendPush = functions.database.ref('/promos').onWrite(event => { 
var topic = "deals_notification"; 
let projectStateChanged = false; 
let projectCreated = false; 
let projectData = event.data.val(); 

if (((event.data.numChildren())-(event.data.previous.numChildren()))>0) { 
    let msg="notification arrived" 
    let payload = { 
     data: { 
      "msg":msg 
     } 
    }; 

admin.messaging().sendToTopic(topic, payload).then(function(response) { 
console.log("Successfully sent message:", response); 
}).catch(function(error) { 
console.log("Error sending message:", error); 
}); 
} 
}); 

はMyFirebaseMessageServiceです:

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 
public class MyFirebaseMessageService extends FirebaseMessagingService { 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 

    sendnotification(remoteMessage.getData().get("msg")); 
} 
private void sendnotification(String body){ 
    Intent intent = new Intent(this, MainActivity.class); 

    PendingIntent pIntent = PendingIntent.getActivity(this,0, intent, 0); 
    Notification.Builder n = new Notification.Builder(this) 
      .setContentTitle("Best Deals") 
      .setContentText(body) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pIntent) 
      .setAutoCancel(true); 

    NotificationManager manager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(0, n.build()); 
} 
} 

編集:ここでは、マニフェストファイルです。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.priya.bestdeals"> 

<uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
    android:maxSdkVersion="25" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<application 
    android:name=".Application" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar"></activity> 
    <activity android:name=".LoginActivity" /> 
    <activity android:name=".RegisterActivity" /> 
    <activity android:name=".Details"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="MainActivity" /> 
    </activity> 
    <activity android:name=".Posts" /> 
    <activity android:name=".Settings" /> 
    <activity android:name=".Postdetails" /> 
    <activity android:name=".IntroActivity" /> 
    <activity 
     android:name=".SplashScreen" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Black.NoTitleBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".WriteActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="MainActivity" /> 
    </activity> 
    <activity android:name=".DeleteActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="MainActivity" /> 
    </activity> 
    <activity android:name=".EditProfile"></activity> 
    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/ic_notifications_black_24dp" /> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" /> 
    <service 
     android:name=".MyFirebaseInstanceIdService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".MyFirebaseMessageService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 

</application> 

ありがとうございます。

+0

受領者をマニフェストに登録してください – portfoliobuilder

+0

ありがとうございます。編集にマニフェストを追加しました。私はfirebase通知を使うのが初めてです。あなたは、アプリケーションが閉じられたときに通知を受け取れるようにするために何を変更すべきかについて言及できますか? @portfoliobuilder – maneesh

+0

私の答え[ここ](https://stackoverflow.com/a/39505298/4625829)(@ BobSnyderの答えにも含まれています)を参照してください。 –

答えて

1

を公式の例を参照してください。これは、アイドル/ Dozeモードにある場合、メッセージを受信するデバイスを起動します:優先度の高いメッセージを送信しても解決しない場合は、あなたが携帯電話のモデルの一つでテストすることができる

const options = { 
    priority: "high" 
}; 

admin.messaging().sendToTopic(topic, payload, options).then(...); 

特定の方法で(最近のタスクリストからスワイプするなどして)殺されると、アプリはStopped Stateに置かれます。詳細は、this answerを参照してください。

This answerには、Huawei/EMOIデバイス用のソリューションが含まれています。あなたは、強制停止されることを免除するためにあなたのアプリをホワイトリストに登録することができます:詳細設定>バッテリー>保護されたアプリ

+0

私はあなたの提案に基づいてコードを更新しました。しかし、アプリは閉じられても通知を表示しません。私は3つの異なる携帯電話でこれを試しましたが、結果は同じです。あなたは、アプリケーションが終了しても通知を表示するように、それ以上の変更を提案してください。 – maneesh

0

実際、あなたは間違った方法でRemoteMessageにアクセスしています。詳細については

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

     if (remoteMessage == null) 
      return; 

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

private void handleDataMessage(JSONObject json) { 
     Log.e(TAG, "push json: " + json.toString()); 

     try { 
      JSONObject data = json.getJSONObject("data"); 

      String title = data.getString("title"); 
      String message = data.getString("message"); 
      boolean isBackground = data.getBoolean("is_background"); 
      String imageUrl = data.getString("image"); 
      String timestamp = data.getString("timestamp"); 
      JSONObject payload = data.getJSONObject("payload"); 

      Log.e(TAG, "title: " + title); 
      Log.e(TAG, "message: " + message); 
      Log.e(TAG, "isBackground: " + isBackground); 
      Log.e(TAG, "payload: " + payload.toString()); 
      Log.e(TAG, "imageUrl: " + imageUrl); 
      Log.e(TAG, "timestamp: " + timestamp); 


      sendNotification(title,message); 


      } catch (JSONException e) { 
      Log.e(TAG, "Json Exception: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
} 
private void sendNotification(String title,String message) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     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) 
       .setSmallIcon(R.drawable.ic_stat_ic_notification) 
       .setContentTitle(title) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

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

あなたはsendToTopic()への通話のためにoptions objectpriority: "high"を指定する必要がグーグル

によって

https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java#L45-L82

関連する問題