I've successfully integrated Parse.com to android app & i can receive the push notification from Data browser and send the notification from android to ios ok.
When i tried to send the notification from ios to android, android cannot receive in my customize MyPushReceiver.
Installation on Parse is written with deviceToken.
I'm using Parse v1.10.2
Here is manifest:
<receiver
android:name=".push.MyPushReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.mypackage" />
</intent-filter>
</receiver>
Here is code in Application:
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_ID);
ParseFacebookUtils.initialize(this);
ParsePush.subscribeInBackground("global", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
}
}
});
ParseInstallation currentInstallation = ParseInstallation.getCurrentInstallation();
currentInstallation.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
}
}
});
Help me!
Related
In my app I want to receive FCM messages even when the app is in foreground. So I've set everything up according to https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive
Yet while my app is in the foreground, the onMessageReceived method is not called. Note that when my app is in background it gets the notifications, so the connection to FCM itself works fine.
application part of my AndroidManifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Timber.i("onMessageReceived") // never called
}
}
I send my notifications using the FCM console. Any idea what I am doing wrong? Why is onMessageReceived not called while my app is in the foreground?
try replacing your service tag in the manifest file with :
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
in the onMessageReceived() refer here you can get the notification / data payload :
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " +
remoteMessage.getNotification().getBody());
}
}
}
You can refer here for more information.
I referred sample application code (https://github.com/googlesamples/android-DirectShare) and I added ChooserTargetService to my application
public class ShareChooserTargetService extends ChooserTargetService {
#Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
Log.d("ShareChooserTargetService", "onGetChooserTargets: ");
ComponentName componentName = new ComponentName(getPackageName(),
ShareActivity.class.getCanonicalName());
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (User user : Users.getAll()) {
Bundle extras = new Bundle();
extras.putInt("user_id", user.id);
targets.add(new ChooserTarget(
user.name,
Icon.createWithResource(this, R.mipmap.ic_user),
componentName,
extras));
}
return targets;
}
In Manifest file I added these lines:
<activity
android:name=".ui.ShareActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".ShareChooserTargetService" />
</activity>
<service
android:name=".ShareChooserTargetService"
android:label="#string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
But ShareChooserTargetService is not called, log isn't printed. I run sample application in my phone and it works perfectly but these code in my app isn't working. I am unable to find what is the issue. Can anyone help please?
I have looked into many questions but i didn't get any push notifications.
I have added all the permissions and have simple receiver. When i debug it shows the device token to be null. In parse the device is detected and the notification is marked to be sent but not received in my device.
I am sharing the code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Parse.initialize(this, "", "");
}
catch (Exception e){
e.printStackTrace();
}
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.e("Tag 1", e == null ? "Succeed to register the app" : "Failed to register the app", e);
String deviceToken = (String) ParseInstallation.getCurrentInstallation().get("deviceToken");
Log.d("Tag 2", "Device token: " + deviceToken);
}
});
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arpan.push_notifivations">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission android:protectionLevel="signature"
android:name="com.example.arpan.push_notifivations.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.arpan.push_notifivations.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.arpan.push_notifivations.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.starter" to match your app's package name.
-->
<category android:name="com.example.arpan.push_notifivations" />
</intent-filter>
</receiver>
</application>
</manifest>
The dependencies i added are
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'
I use parse.com api.
What my problem is :
When other client send push notification I did not receive anything on my broadcastreceiver.
My manifest
<service android:name="com.parse.PushService" />
<receiver
android:name=".MainActivity$MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.yy.xx.UPDATE_STATUS" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
and my receiver in mainactivity is here:
public static class MyReceiver extends BroadcastReceiver {
public final String TAG = "MyReceiver";
#Override
public void onReceive(Context context, Intent intent) {
//this is not working,
try {
if (intent == null)
{
Log.d(TAG, "Receiver intent null");
}
else
{
String action = intent.getAction();
if (action.equals("com.yy.xx.UPDATE_STATUS"))
{
//....
}
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
and my push notification send code is here:
JSONObject obj;
try {
obj =new JSONObject();
obj.put("action","com.yy.xx.UPDATE_STATUS");
obj.put("customdata","message...");
push_p = new ParsePush();
#SuppressWarnings("rawtypes")
ParseQuery query = ParseInstallation.getQuery();
// Notification for Android users
//query.whereEqualTo("deviceType", "android");
push_p.setQuery(query);
push_p.setData(obj);
push_p.setChannel("channel_111");
push_p.sendInBackground();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I read parse.com api and tutorial but I do not understand what is the problem.
thanks in advance
Add all this declarations in manifest
<!-- PARSE SERVICE AND RECEIVERS -->
<service
android:name="com.parse.PushService"
android:enabled="true" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:enabled="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.app" />
</intent-filter>
</receiver>
<receiver android:name=".MainActivity$MyReceiver" >
<intent-filter>
<action android:name="com.example.update.UPDATE_STATUS" />
</intent-filter>
</receiver>
And add permissions
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />
<permission
android:name="com.example.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
I'm unable to get the text from the push notification powered by Parse.com when I receive it
my onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
Parse.initialize(this, "HDTss5xhd88H0uTmNBhCx4wWxfxVht6Cm5hWhbt8", "Y3KGXmndgySCla8yhrzgvT2d5uIJhg0WDI8MJyPB");
PushService.setDefaultPushCallback(getApplicationContext(), MainActivity.class);
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("file:///android_asset/Qiyas/index.html");
}
my Manifest.xml
<receiver android:name="pushReceiver" >
<intent-filter>
<action android:name="MyAction" >
</action>
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.myfirstpush" />
</intent-filter>
</receiver>
my pushReceiver.class
public class pushReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data")
: "";
JSONObject jObject;
try {
jObject = new JSONObject(message);
Toast toast = Toast.makeText(context, jObject.getString("alert")
+ jObject.getString("title") + jObject.getString("action"),
Toast.LENGTH_LONG);
toast.show();
Log.d("Riceieved",
jObject.getString("alert") + jObject.getString("title")
+ jObject.getString("action"));
} catch (JSONException e) {
e.printStackTrace();
}
}
I receive the push notification, but i want to extract the text in it
Thank you,
You need to correct you AndroidManifest.xml -
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="Your_Package_Name" />
</intent-filter>
</receiver>
<receiver android:exported="false" android:name="PushReceiver_Class">
<intent-filter>
<!-- Choose According to need. -->
<action android:name="Your_Package_Name.UPDATE_STATUS" />
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
You can find more information Here.