i am using parse 1.13.0 in nodechef.com i tried setting my app to send notification but i getting some problems.
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.path" >
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.my.path.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.my.path.permission.C2D_MESSAGE" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/hsfTheme">
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="#string/parse_app_id" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="#string/parse_client_key" />
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="#drawable/logo" />
<service android:name="com.parse.PushService" />
<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: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.my.path" />
</intent-filter>
</receiver>
<!-- activity main. -->
<activity
android:name=".A0"
android:configChanges="keyboardHidden|screenLayout|orientation"
android:immersive="true"
android:screenOrientation="portrait"
android:theme="#style/hsfTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".A1" />
<activity android:name=".A2" />
<activity android:name=".A3" />
<activity android:name=".utils.A4" />
<!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
in my application class i have: ParseInstallation.getCurrentInstallation().saveInBackground();
DeviceToken is empty in _Installation class
Push not working
All i need is send push notification from my app, in some actions i would like notify all users or some users using Installation channels. Don't care if i will use "parse push" or gcm, just need a simple way to send push notifications from app.
EDIT
Push from parse-dashboard is working now, but i cant send push notification from client. I tried a push and got "unauthorized: enable master key". So what i need to do ?
EDIT 2
I created a cloud code but its looks not working get values from request.params:
Parse.Cloud.define("sendPush", function(request,response) {
var _locale = "en-US";//request.params.locale;
var _message = "test";//request.params.message;
var query = new Parse.Query(Parse.Installation);
query.equalTo("localeIdentifier", _locale);
Parse.Push.send({
data: {
alert: _message,
badge: "Increment",
sound: "default"
},
where: query
}, {
useMasterKey: true
})
.then(function() {
response.success("Push Sent!");
}, function(error) {
response.error("Error while trying to send push " + error.message);
});
});
EDIT 3
HashMap<String,String> map = new HashMap<String, String>();
map.put("locale","en-US");
map.put("message","Text added");
ParseCloud.callFunctionInBackground("sendPush",map, new FunctionCallback<Object>() {
#Override
public void done(Object object, ParseException e) {
// toast done
}
});
after investigate it a bit i noticed that in order to send push you must create cloud code function and use your master key (by sending userMasterKey=true parameter to the send push function.
I describe my answer in here so please go through the steps and it should work for you.
Related
AndroidManifest.xml
This is My android manifest file, notifications comes properly but not showing popup like other applications. Application is in foreground sate or background state popup is not showing notifications are show in notification tray only.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pushnotificationexample">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:name=".MainApplication"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<!-- <meta-data android:name="com.dieam.reactnativepushnotification.channel_create_default" android:value="true"/>-->
<!-- Change the value to true to enable pop-up for in foreground on receiving remote notifications (for prevent duplicating while showing local notifications set this to false) -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
android:value="true"/>
<!-- Change the resource name to your App's accent color - or any other color you want -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="#color/white"/> <!-- or #android:color/{name} to use a standard color -->
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
Firebase Push Notification Listener
messaging().onNotificationOpenedApp(remoteMessage => {
console.log(
'Notification caused app to open from background state:',
remoteMessage.notification,
);
navigation.navigate(remoteMessage.data.type);
});
// Check whether an initial notification is available
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage.notification,
);
setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
}
setLoading(false);
});
Above Listener Will Be Handled When Application Is In Background State OR Killed.
But When Application Is In Foreground Use Code Below
messaging().onMessage(remoteMessage => {
PushNotification.localNotification({
id: "1", ....
// Configure Local Notification
})
}
)
Firebase Referece
Firebase PushNotification PayLoad
My App was getting notification only when app is open and in background. but i require notifications should reach even app is closed. How could i do this in reactnative using react-native-fcm?
My code was below please refer to it
FCM.getInitialNotification().then(notif=>console.log(notif));
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
if(notif.local_notification){
//this is a local notification
return;
}
if(notif.opened_from_tray){
//app is open/resumed because user clicked banner
console.log('clicked');
return;
}
this.showLocalNotification(notif);
});
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => {
console.log(token)
// fcm token may not be available on first load, catch it here
});
showLocalNotification(notif) {
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
priority: "high",
click_action: notif.click_action,
show_in_foreground: true,
local: true
});
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
<receiver android:enabled="true" android:exported="true" android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="fcm.ACTION.HELLO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="XXXXXX"/>
</application>
Your problem is how you are pushing your notification.
The FCM will recognise and popup only notifications which contains the attribute "notification".
If you need the command
this.showLocalNotification(notif);
to show your notification it means your are managing notifications locally, hence, your closed app won't manage it.
As a result, please check how you are sending your notification to the cloud and be sure the event "FCMEvent.Notification" is triggered only if you dont have a "notification" attribute.
Ex:
noteA = {notification: {title: "Notification Title", body:"Notification body"}, data: {optional: "content"}};
noteB = {title: "Notification Title", body:"Notification body"}, data: {optional: "content"}};
Result:
noteA: Should popup a notification without triggering FCMEvent.Notification
noteB: Should trigger FCMEvent.Notification and if this.showLocalNotification(notif) is invoked a local notification will be shown.
I couldn't find a page or question where it can tell me how to make a notification without the app running in background or it being opened.
It would be great if anyone could help. (I'm using Android)
First step is to create a Parse account and get your Application Id and Client Key.
After that you have to create your own custom Application class by creating a class that extends Application and then override onCreate (just like you would any activity) and place that line in.
public class MyApplication extends Application {
public void onCreate() {
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
You also have to tell the manifest that you are using a custom application class. You can do this by, in your AndroidManifest.xml file, you will have to set the name element to the location of you new Application class:
<application
android:name="com.packageName.example.MyApplication"
android:label="#string/app_name"
android:logo="#drawable/ic_launcher_no_text" >
In the Manifest file
Declare the following permissions:
<permission android:name="com.packagename.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.packagename.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET"/>
Also declare a service and receiver in the manifest:
<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" />
<category android:name="com.packagename.android" />
</intent-filter>
</receiver>
Change com.packagename to your package name too!
Finally go to your parse account and try sending a push notification from there.
I'm trying to set up push notifications in an app for the first time. I think the server side is OK (the message send to google comes back with status code 200, and I see a success result in the response body).
But the device never does anything :(
Manifest is set as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/appName"
android:theme="#style/AppTheme" >
<activity
android:name="my.package.activity.MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="my.package" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<service
android:name=my.package.NotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
</manifest>
And the NotificationListenerService is like this:
public class NotificationListenerService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d("MyApp", "message);
Notification.ShowNotification("test", getApplicationContext());
}
}
I think that's all I need according to https://developers.google.com/cloud-messaging/android/client (asides from the stuff to handle reset tokens which I've not added yet. My token registration seems to work as I can see the token in the dev console data store.)
I've tried looking at logcat but nothing obvious seems to appear. Do I need to "start" the service in some way? The documentation suggests not... I'm obviously missing something fundamental though!
When you send a message, your server normally gets a message_id as a response. Using the diagnostics tool with the message_id could be useful to make sure that GCM correctly relayed the message.
Is Google Play Services up-to-date on your device ?
I have been eating my brains for hours and cannot find an answer.
My problem is that I am trying to send push notifications to my android device from the Parse.com console on the web and when I send a message there is no problem but if I try to send JSON then I will never see the notification on my device.
I am trying to send a JSON message as simple as this-> {"x":"1"}
my relevant manifest code
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:protectionLevel="signature"
android:name="my.app.package.permission.C2D_MESSAGE" />
<uses-permission android:name="my.app.package.permission.C2D_MESSAGE" />
<!-- Parse push notification receiver -->
<service android:name="com.parse.PushService" />
<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: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="my.app.package" />
</intent-filter>
</receiver>
<receiver android:name="my.app.package.parse.NotificationReceiver" 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>
my.app.package.parse.NotificationReceiver exists normally and handles the application opening successfully for the message notifications
and my Application's onCreate() runs the following
Parse.enableLocalDatastore(this);
Parse.initialize(this, "..........", ".............");
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
ParsePush.subscribeInBackground("everything", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
I have run out of ideas so Please help!
My next idea solved my problem.
It seems that a JSON message should include a tag "alert" with the value of the text that is needed to be shown as a notification.
If "alert" tag is not specified then the notification is not shown. So my {"x":"1"} would not show up but
{ "x": "1", "alert": "show me to the user" }
is showing up normally.
Unfortunately I did not find this anywhere in parse user guide, so PLEASE Parsers add this to the guides to let other developers not waste time.
Thanks
Try to specify that the POST you are sending is of type application/json
By default, the POST has a media type of application/x-www-form-urlencoded. This will obviously not be recognized as JSON by the machine.