0

私はserverless FCMプッシュ通知を使用しているアンドロイドアプリケーションを作っています。したがって、2種類の通知を送信する必要があります。最初に、顧客から現在の場所の1〜2km内に登録されたすべての店主にブロードキャスト通知を送信し、2回目は、その特定の店主の返信です顧客のみ。私はこのコードに固執しており、問題を解決することはできません。私のnodes.jsとアンドロイドコードは以下の通りです。FCMでは、どのようにして特定のグループのユーザーと1人のユーザーにプッシュ通知を送信できますか?

//import firebase functions modules 
const functions = require('firebase-functions'); 
//import admin module 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 


// Listens for new messages added to messages/:pushId 
exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite(event => { 

    console.log('Push notification event triggered'); 

    // Grab the current value of what was written to the Realtime Database. 
    var valueObject = event.data.val(); 

    if(valueObject.photoUrl != null) { 
     valueObject.photoUrl= "Sent you a photo!"; 
    } 
console.log('Push notification 1'); 
    // Create a notification 
    const payload = { 
     notification: { 
      title:valueObject.name, 
      body: valueObject.text || valueObject.photoUrl, 
      sound: "default" 

     }, 
    }; 
console.log('Push notification 2'); 
    //Create an options object that contains the time to live for the notification and the priority 
    const options = { 
     priority: "high", 
     timeToLive: 60 * 60 * 24 

    }; 

console.log('Push notification 3'); 
    return admin.messaging().sendToTopic("pushNotifications", payload, options); 
}); 






public class CloudMain extends BaseActivty { 



    public static final String ANONYMOUS = "anonymous"; 
    public static final int DEFAULT_MSG_LENGTH_LIMIT = 10; 
    public static final int RC_SIGN_IN = 1; 
    private static final int RC_PHOTO_PICKER = 2; 


    private ListView mMessageListView; 
    private MessageAdapter mMessageAdapter; 
    private ProgressBar mProgressBar; 
    private ImageButton mPhotoPickerButton; 
    private EditText mMessageEditText; 
    private Button mSendButton; 

    private String mUsername; 

    private FirebaseDatabase mDatabase; 
    private DatabaseReference mMessagesReference; 
    private ChildEventListener mChildEventListener; 
    private FirebaseAuth mFirebaseAuth; 
    private FirebaseAuth.AuthStateListener mAuthStateListener; 
    private FirebaseStorage mStorage; 
    private StorageReference mChatPhotosStorageRef; 
    private FirebaseUser user; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_cloud_main); 
     Intent intent = getIntent(); 
     mDatabase = FirebaseDatabase.getInstance(); 
     mFirebaseAuth = FirebaseAuth.getInstance(); 
     mStorage = FirebaseStorage.getInstance(); 
     FirebaseUser user = mFirebaseAuth.getCurrentUser(); 
     String userId = user.getUid(); 
     mUsername = user.getDisplayName() ; //ANONYMOUS; 
     mMessagesReference = mDatabase.getReference().child("users").child(userId).child("messages"); 
     mChatPhotosStorageRef = mStorage.getReference().child("users").child(userId).child("chat_photos"); 

     FirebaseMessaging.getInstance().subscribeToTopic("pushNotifications"); 
     // Initialize references to views 
     mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 
     mMessageListView = (ListView) findViewById(R.id.messageListView); 
     mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton); 
     mMessageEditText = (EditText) findViewById(R.id.messageEditText); 
     mSendButton = (Button) findViewById(R.id.sendButton); 


     // Initialize message ListView and its adapter 
     List<Message> messages = new ArrayList<>(); 
     mMessageAdapter = new MessageAdapter(this, R.layout.item_message, messages); 
     mMessageListView.setAdapter(mMessageAdapter); 

     // Initialize progress bar 
     mProgressBar.setVisibility(ProgressBar.INVISIBLE); 

     // ImagePickerButton shows an image picker to upload a image for a message 
     mPhotoPickerButton.setOnClickListener(new View.OnClickListener() { 
      // TODO: Fire an intent to show an image picker 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
       intent.setType("image/jpeg"); 
       intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 
       startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER); 

      } 
     }); 

     // Enable Send button when there's text to send 
     mMessageEditText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       if (charSequence.toString().trim().length() > 0) { 
        mSendButton.setEnabled(true); 
       } else { 
        mSendButton.setEnabled(false); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
      } 
     }); 
     mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)}); 


     // Send button sends a message and clears the EditText 
     mSendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // TODO: Send messages on click 

       Message message = new Message(mMessageEditText.getText().toString(), mUsername, null); 
       mMessagesReference.push().setValue(message); 

       mMessageEditText.setText(""); 
      } 
     }); 


     mAuthStateListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 

       FirebaseUser user = firebaseAuth.getCurrentUser(); 
       String userId = user.getUid(); 
       Log.d("onAuthStateChanged: ",userId); 
       if (user != null) { 


       } else { 

       } 
      } 
     }; 
    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     mFirebaseAuth.removeAuthStateListener(mAuthStateListener); 
     detachDatabaseReadListener(); 
     mMessageAdapter.clear(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RC_SIGN_IN) { 
      if (resultCode == RESULT_OK) { 
       Toast.makeText(this, "Signed in.", Toast.LENGTH_SHORT).show(); 
      } else if (resultCode == RESULT_CANCELED) { 
       Toast.makeText(this, "Sign in canceled.", Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     } else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 
      StorageReference photoRef = 
        mChatPhotosStorageRef.child(selectedImageUri.getLastPathSegment()); 

      photoRef.putFile(selectedImageUri) 
        .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { 
         @Override 
         public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
          Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
          Message message = new Message(null, mUsername, downloadUrl.toString()); 
          mMessagesReference.push().setValue(message); 
         } 
        }); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mFirebaseAuth.addAuthStateListener(mAuthStateListener); 
    } 



    private void attachDatabaseReadListener() { 
     if (mChildEventListener == null) { 
      mChildEventListener = new ChildEventListener() { 
       @Override 
       public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
        Message message = dataSnapshot.getValue(Message.class); 
        mMessageAdapter.add(message); 
       } 

       @Override 
       public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
       } 

       @Override 
       public void onChildRemoved(DataSnapshot dataSnapshot) { 
       } 

       @Override 
       public void onChildMoved(DataSnapshot dataSnapshot, String s) { 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 
       } 
      }; 
      mMessagesReference.addChildEventListener(mChildEventListener); 
     } 
    } 

    private void detachDatabaseReadListener() { 
     if (mChildEventListener != null) { 
      mMessagesReference.removeEventListener(mChildEventListener); 
      mChildEventListener = null; 
     } 
    } 
} 
+0

こんにちはkAnwA1。これは少し広いです。あなたはどこに問題があるのか​​を具体的に指摘できますか? –

+0

私は上記のコードで通知を送信できません、何が間違っていますか? – kAnwAl

答えて

0

サーバから500エラーですか?あなたはもっと素直に体を縛ります。

body: Json.stringify(**content**); 
関連する問題