I know that this question has been asked a lot, but many also went unanswered.
However, I use Parse.com to send push-notifications to devices. On iOS it works fine. But it's not receives on Android devices.
When I looking at Push tab on service page I see that no one push wasn't sent. Value in 'Pushes Sent' column is 0 (zero).
Then I sent push through GCM (gcm-http.googleapis.com) and my device received the message.
Perhaps the problem may be in settings of Parse.com?
I was added Sender ID (Project number) & API Key from Google.
My AndroidManifest.xml immediately before the opening </application> tag:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission android:name="my.app.name.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my.app.name.permission.C2D_MESSAGE" />
My AndroidManifest.xml immediately before the closing </application> tag:
<meta-data android:name="com.parse.push.notification_icon" android:resource="#drawable/ic_stat_bt"/>
<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.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="my.app.name" />
</intent-filter>
</receiver>
My ApplicationClass
package my.app.name;
import android.util.Log;
import android.os.Bundle;
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.SaveCallback;
import com.parse.ParseException;
public class MyApplication extends android.app.Application {
#Override
public void onCreate() {
super.onCreate();
// Parse
Parse.initialize(this, "", "");
ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully installed.");
ParsePush.subscribeInBackground("", 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);
}
}
});
} else {
Log.e("com.parse.push", "failed to install", e);
}
}
});
}
...
On the advice of Clayton Oliveira I noticed, when I install my app and initialize it first time I get my token and pushes works fine. But when I open my app again I get another token and pushes are not working until I delete all data from Parse, uninstall my app & install it again.
Any ideas?
I was facing the same problem just now ! Here's what did the trick:
I uninstalled my app and deleted all the data previously stored on Parse (Installation, Session and User).
After that, just install your app again and if you did everything right from the Parse tutorial: https://www.parse.com/tutorials/android-push-notifications
Try send another Test Push ;)
If you do not add the permission of the message then add it in to your manifest file.
<uses-permission android:name="your.package.name.permission.C2D_MESSAGE"/>
and add this firebase lib in app's build.gradle file:
compile 'com.google.firebase:firebase-messaging:11.0.4'
Related
Today I've implemented Parse.com Push service in my app.
I've noticed that the push notifications are being sent only when the app is open. I've also noticed that whenever I'm turning on my device there is a message says "Unfortunately, tof has stopped". The Logcat says Unable to start receiver.
The full Logcat:
java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins$Android.applicationContext()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2952)
at android.app.ActivityThread.access$1800(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins$Android.applicationContext()' on a null object reference
at com.parse.Parse.checkContext(Parse.java:440)
at com.parse.Parse.getApplicationContext(Parse.java:270)
at com.parse.ManifestInfo.getContext(ManifestInfo.java:324)
at com.parse.ManifestInfo.getPackageManager(ManifestInfo.java:328)
at com.parse.ManifestInfo.getPackageInfo(ManifestInfo.java:358)
at com.parse.ManifestInfo.deviceSupportsGcm(ManifestInfo.java:446)
at com.parse.ManifestInfo.getPushType(ManifestInfo.java:212)
at com.parse.PushService.startServiceIfRequired(PushService.java:222)
at com.parse.ParseBroadcastReceiver.onReceive(ParseBroadcastReceiver.java:19)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2945)
at android.app.ActivityThread.access$1800(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
I've tried to search on the Web but I didn't find any solution. I guess that it's something about the receiver background operation.
I need an answer as quickly as possible, so if you know a bit about Parse SDK and it's push topic so please, help me 'cause I need to solve this today.
This is my code.
MainActivity.java:
ParsePush parsePush = new ParsePush();
ParseQuery pQuery = ParseInstallation.getQuery(); // <-- Installation query
pQuery.whereEqualTo("username", User_Name);
parsePush.sendMessageInBackground(message, pQuery);
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.intap.tof.permission.C2D_MESSAGE" />
<uses-permission android:name="com.intap.tof.permission.C2D_MESSAGE" />
...
<service android:name="com.parse.PushService" />
<receiver android:name="com.intap.tof.Receiver"
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.intap.tof" />
</intent-filter>
</receiver>
I also have this line on Receiver.java:
package com.intap.tof;
import android.content.Context;
import android.content.Intent;
import com.parse.ParsePushBroadcastReceiver;
public class Receiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
I need major help right now!! Please help me!
Follow the steps below. This code works fine and run the receiver on background. The original answer link: https://www.parse.com/apps/quickstart#parse_push/android/native/existing
MainActivity.java file
public void onCreate() {
Parse.initialize(this, "APPLICATION ID", "CLIENT KEY");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
AndroidManifest.xml file
<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.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.parse.starter" />
</intent-filter>
</receiver>
Permissions.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />
Have a look at this.
public class MainApplication extends Application {
private static MainApplication instance = new MainApplication();
public MainApplication() {
instance = this;
}
public static Context getContext() {
return instance;
}
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, " ", " ");
PushService.setDefaultPushCallback(this, FabulaClient.class);
PushService.subscribe(this, "Barca", FabulaClient.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
taken from https://www.parse.com/questions/cannot-send-push-to-android-after-app-is-closed-until-screen-unlock
I've understood why I don't get the push notifications on background.
I've initialized parse sdk twice - on the OpenActivity.java and on ParseApplication.java. I've removed it from the OpenActivity.java and added this code line:
android:name=".ParseApplication"
To the application attribute on the AndroidManifest.xml and now I can get push notifications on background.
I'm using Parse Unity SDK for Android.
I've managed to register the device successfully.
void Start() {
//Parse Installation
if (ParseInstallation.CurrentInstallation != null && !string.IsNullOrEmpty(ParseInstallation.CurrentInstallation.DeviceToken))
{
Debug.Log("Device Token : " + ParseInstallation.CurrentInstallation.DeviceToken);
}
else
{
ParseInstallation installation = ParseInstallation.CurrentInstallation;
installation.Channels = new List<string> { Config.Instance.GetUserInfo().GetEmail() };
installation.SaveAsync().ContinueWith(t => {
if (t.IsFaulted || t.IsCanceled)
{
Debug.Log("Push subscription failed.");
}
else
{
Debug.Log("Push subscription success.");
}
});
//installation.
}
}
Only to discover that the notification is "received" but not being displayed neither in the app nor in the notifications bar.
I/GCM ( 1285): GCM message com.ahmed.app 0:1433935471473270%3f8fc5dbf9fd7ecd
I/ParsePushService( 7057): Push notification received. Payload: {"alert":"test","push_hash":"098f6bcd4621d373cade4e832627b4f6"}
I/ParsePushService( 7057): Push notification is handled while the app is foregrounded.
W/GCM-DMM ( 1285): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.ahmed.app (has extras) }
Also, Unity does not receive the notification (see code)
void Awake() {
ParsePush.ParsePushNotificationReceived += (sender, args) => {
#if UNITY_ANDROID
AndroidJavaClass parseUnityHelper = new AndroidJavaClass("com.parse.ParseUnityHelper");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
//Debugging the payload
Debug.Log("Calling Parse from Unity and Payload is : " + args.StringPayload);
// Call default behavior.
parseUnityHelper.CallStatic("handleParsePushNotificationReceived", currentActivity, args.StringPayload);
#endif
};
}
This event doesn't get triggered.
Here's my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ahmed.app" android:theme="#android:style/Theme.NoTitleBar" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:protectionLevel="signature" android:name="com.ahmed.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.ahmed.app.permission.C2D_MESSAGE" />
<application android:icon="#drawable/app_icon" android:label="#string/app_name" android:debuggable="false">
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="#string/app_name" android:screenOrientation="portrait" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="4030500" />
<activity android:name="com.outlinegames.unibill.PurchaseActivity" android:label="#string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<service android:name="com.parse.ParsePushService" />
<receiver android:name="com.parse.ParsePushBroadcastReceiver" 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.ahmed.app" />
</intent-filter>
</receiver>
</application>
Any idea what's the problem?
I have answered the same kind of question, I just wanted to let you know.
Parse Unity Push Sample not working
Basically, this line of your code is wrong:
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
You have to replace it by
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("com.unity3d.player.UnityPlayerNativeActivity");
Rather than downloading the SDK manually, I've downloaded the Parse Push sample project and copied the files to my project. Using the SDK 1.5.2 sample project, it works now.
I'm trying to create a simple push notification application using parse, here is the code that I've used from the documentation:
package com.parse.starter;
import android.app.Application;
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseCrashReporting;
import com.parse.ParseUser;
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// Initialize Crash Reporting.
ParseCrashReporting.enable(this);
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(this, "-", "-");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// Optionally enable public read access.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
Activity:
public class ParseStarterProjectActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.starter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="21"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />
<application
android:name=".ParseApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:allowBackup="true">
<activity
android:name=".ParseStarterProjectActivity"
android:label="#string/app_name" >
<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.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.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.parse.starter" />
</intent-filter>
</receiver>
</application>
</manifest>
I try to send push notifications using the parse.com website console, but I dont receive anything in the app. Am I missing a part of the code?
EDIT: My package name is "com.parse.starter"!! its in AndroidManifest.xml
For whatever reason, Parse has two tutorials; one is complete and the other isn't. Guess which one you found.
The complete one is here:
https://www.parse.com/tutorials/android-push-notifications
Essentially, you are missing step 5. In your Application class, in the onCreate method...
ParsePush.subscribeInBackground("", 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);
}
}
});
In your manifest it clearly says...
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />
and
<!--
IMPORTANT: Change "com.parse.starter" to match your app's package name.
-->
<category android:name="com.parse.starter" />
Go to this parse create new application.Enter your application name and
click create app.
Then go to settings in that app.Copy your application id and client
key and paste it in your code.
Then you have to enable the Client push in settings->push.
Click Push->send a push ->Push Quick
Start->Android->Native(Java)->Existing Project->Then you have to
follow all the instructions placed in this Quick Start Link.
You have to add the parse 1.9.3 jar in File ->project structure ->App
->Dependencies-> + ->File Depency ->add the parse 1.9.3 jar.
The Application Class have to be like this:
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;
public class Application extends android.app.Application {
public Application() {
}
#Override
public void onCreate() {
super.onCreate();
// Initialize the Parse SDK.
Parse.initialize(this, "", ""); //app id and client key
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
Output:
You can see the verification here:
In your Application class, paste this:
Parse.initialize(this, "...", "...");
ParseInstallation.getCurrentInstallation().saveInBackground();
The 2nd line is missing. This is a similar solution to Murkaeus's answer.
Source: https://www.parse.com/apps/quickstart#parse_push/android/native/new
I've been trying to make a simple Phonegap app that receives Parse Push notifications on Android.
I've read a number of tutorials and read through related issues here and elsewhere but i can't seem to get it to work without crashing.
I have the latest of Java JDK, Cordova + Node JS + plugman, Android API 19 (seems to be required by cordova), Apache ant, etc...
Bottom line: I can start the app. I can receive the notification just fine. But when I tap the notification to go back to the app it always crashes.
Question? Can anyone explain why this is happening? Or even better if you have a sample project that works? Any help would be greatly appreciated.
References:
https://parse.com/tutorials/android-push-notifications
http://www.raymondcamden.com/2012/10/10/PhoneGap-Parsecom-and-Push-Notifications -- a bit out of date
How do I get Parse.com Push Notifications working in a Cordova/Phonegap Android app?
apache cordova app crashes after receiving parse.com push notification
Sources:
AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.company.challenger" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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:name="com.company.challenger.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.company.challenger.permission.C2D_MESSAGE" />
<application android:hardwareAccelerated="true" android:icon="#drawable/icon" android:label="#string/app_name" android:name="com.company.challenger.ChallengerApplication">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="#string/app_name" android:launchMode="singleTop" android:name="ChallengerApplication" android:theme="#android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="#string/launcher_name">
<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.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.company.challenger" />
</intent-filter>
</receiver>
<receiver android:exported="false" android:name="com.parse.ParsePushBroadcastReceiver">
<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>
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
</manifest>
CordovaApp.java
package com.company.challenger;
import com.parse.ParseAnalytics;
import android.os.Bundle;
import org.apache.cordova.*;
public class CordovaApp extends CordovaActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
ParseAnalytics.trackAppOpened(getIntent());
}
}
ChallengerApplication.java
package com.company.challenger;
import android.app.Application;
import android.content.Context;
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;
import com.company.challenger.CordovaApp;
public class ChallengerApplication extends Application
{
private static ChallengerApplication instance = new ChallengerApplication();
public ChallengerApplication() {
instance = this;
}
public static Context getContext() {
return instance;
}
#Override
public void onCreate() {
super.onCreate();
// register device for parse
Parse.initialize(this, "app key", "client key");
PushService.setDefaultPushCallback(this, CordovaApp.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
Proper answer this time...
I basically started from scratch following the latest parse tutorial and making sure the initialize parse in the MainApplication and not in your javascript.
This is the gist:
Create a MainApplication.java with a skeleton like this:
package com.{your_company}.{your_app};
import android.app.Application;
import android.content.Context;
import com.parse.Parse;
import com.parse.ParsePush;
import com.parse.SaveCallback;
import com.parse.ParseException;
import com.{your_company}.{your_app}.MainActivity;
public class MainApplication extends Application
{
#Override
public void onCreate() {
super.onCreate();
// parse initialization goes here
}
}
Add the following attribute to your application tag in AndroidManifest.xml
<application ... android:name="com.{your_company}.{your_app}.MainApplication">
Follow this tutorial from Parse.com which is fairly decent https://parse.com/tutorials/android-push-notifications .
The final onCreate in MainApplication.java should look something like this:
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(app);
Parse.initialize(app, "{app_key}", "{client_key}");
ParsePush.subscribeInBackground("", 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);
}
}
});
}
From this experience I found the following insights:
Not recommended to use any parse push plugins floating around because
there are all out of date using deprecated calls or simply did not
work out of the box.
The Parse initialize must occur in the MainApplication and cannot occur in the javascript. (I don't have the reference for this one, I came
across it somewhere down the line when frantically researching).
Always use the latest Parse SDK and use ParsePush (the new service) and not PushService which is depricated.
Hopefully this helps.
Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AlarmActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="CallReciver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:enabled="true"
android:name=".AlarmService">
</service>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS">
</uses-permission>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
Receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class OnBootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("Test","booot");
Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
}
}
Receiver doesn't work. I turn off and on my device and nothing happens.
SMS And Call Receiver in this project work good.
SMS Receiver and CallReceviver - works good.
First post updated - added full manifest.
If you have HTC device you also need to register for "android.intent.action.QUICKBOOT_POWERON". So the entry in manifest should be:
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
On my HTC, if I turn off the device and turn it on for a while I got QUICKBOOT_POWERON and no BOOT_COMPLETED.
If I turn off the device and remove the battery for a while - I got BOOT_COMPLETED after start.
Put permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Also know that in Android >= 3.1 the app gets installed in 'stopped' state and will not get boot and shutdown events until the user 'does something' with the app at least once. See this post on the topic.
Try this::
<receiver android:enabled="true" android:exported="true"
android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Cheers...!!!