简体   繁体   中英

Parse android push notifications marked as sent, but not seen on device

Edit: Solved! The problem was that I was running the app in an emulator, and the parse deviceToken was not generated. I ran it on an actual device and now I am receiving the notifications!

I've been trying to make Parse push notifications work on android. On the Parse.com dashboard I see that my app is registered and connected to Parse, but when I send a push notification Parse says it was sent, but it doesn't show up on the device/emulator.

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Manifest:

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


<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" />
<permission android:protectionLevel="signature"
    android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<application
    android:name=".News"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".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" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>
    <meta-data android:name="com.parse.push.notification_icon"
        android:resource="@mipmap/ic_launcher"/>
</application>

</manifest>

New class: "News"

package XXXX.XXXX.XXXX.XXXX;

import android.app.Application;

import com.parse.Parse;
import com.parse.ParseInstallation;

public class News extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Parse.initialize(this, "XXXXXXXXXXXXXX", "XXXXXXXXXXXXXXX");
        ParseInstallation.getCurrentInstallation().saveInBackground();

        ParsePush.subscribeInBackground("---");
    }
}

I am new to Parse, and Iv'e tried a lot of things I saw found online (I apologise if my code is a mess), but I can't seem to be able to find a solution for this.

Any help would be greatly appreciated, Thank you.

(I've seen people putting the Parse stuff in a different class, and I've tried it myself, but I don't really understand what to do or how it works)

It is best to initialise Parse in an Application subclass, rather than an Activity .

For example, when a push is received, your BroadcastReceiver will be activated, which in turn will trigger the Application to run first. This will initialise Parse as required.

Bear in mind you also need to subscribe to the correct channel in order to receive anything.

So here is an example application class:

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Parse.initialize(this, "XXXXXXXXX", "XXXXXXXXX");
        ParseInstallation.getCurrentInstallation().saveInBackground();

        ParsePush.subscribeInBackground("<Channel Name>");
    }
}

It needs to be declared in your manifest:

<application
    android:name=".MyApp">
... etc ...
</application>

If you are using Gradle to build with (or Android Studio), then there is a useful shortcut for making sure that your Parse Push permissions and receivers are declared correctly.

In your Manifest, declare your permissions using the ${applicationId} keyword:

<permission
    android:name="${applicationId}.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />

And your broadcast receiver like this:

<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="${applicationId}" />
    </intent-filter>
</receiver>

Gradle will automatically expand the ${applicationId} into the correct string before building the app.

This becomes essential when you start building different app flavors, as you then need to handle different application IDs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM