ketan
(Ketan)
March 17, 2016, 7:37am
1
I am able to receive push notification by using the default “BackendlessBroadcastReceiver” receiver.
I intent to receive PUSH notification in a custom receiver.
I have declared my receiver in Manifest as below-
<receiver
android:name=".PushReceiver"
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="xxx.xxx.xxx" />
</intent-filter>
</receiver>
PushReceiver as follows below -
public class PushReceiver extends BackendlessBroadcastReceiver {
@Override
public boolean onMessage(Context context, Intent intent) {
CharSequence tickerText = intent.getStringExtra( PublishOptions.ANDROID_TICKER_TEXT_TAG );
CharSequence contentTitle = intent.getStringExtra( PublishOptions.ANDROID_CONTENT_TITLE_TAG );
CharSequence contentText = intent.getStringExtra( PublishOptions.ANDROID_CONTENT_TEXT_TAG );
String message = intent.getStringExtra( PublishOptions.MESSAGE_TAG );
android.support.v4.app.NotificationCompat.Builder mBuilder =
new android.support.v4.app.NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setTicker(tickerText)
.setAutoCancel(true)
.setOngoing(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
return true;
}
}
I’m not sure whether PUSH notification are received or not but the notification is not displayed.
Hi Ketan,
Sounds like you’re asking us to debug your code… We’d be happy to do it, but that would be outside of support. Contact sales@backendless.com to discuss your options.
Mark
ketan
(Ketan)
March 17, 2016, 8:40am
3
Hi Mark. Nah I would like to know that what I did is it correct or not?
Ketan,
Please understand, we provide support for our APIs and backend. When you start customizing things this goes outside of the support charter. If I says “yes, what you did is correct”, but if things will not work - you will say “what did I do wrong?”. The source code for our receive is right there. You’re welcome to debug your code (and ours if you want to), however, when it comes to reviewing derivatives from our code, this is where we draw the line for free support.
Mark
ketan
(Ketan)
March 17, 2016, 8:56am
5
Hello Mark, I’m interested in displaying the Push “Message” in Android Activity. Can you please tell me how to do that using default “BackendlessBroadcastReceiver”.
Sure, take a look at the following code:
/*
* ********************************************************************************************************************
* <p/>
* BACKENDLESS.COM CONFIDENTIAL
* <p/>
* ********************************************************************************************************************
* <p/>
* Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
* <p/>
* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers,
* if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its
* suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret
* or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from Backendless.com.
* <p/>
* ********************************************************************************************************************
*/
package com.backendless.push;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import com.backendless.Backendless;
import java.util.HashMap;
import java.util.Map;
public class BackendlessBroadcastReceiver extends BroadcastReceiver implements PushReceiverCallback
{
private static final String TAG = BackendlessBroadcastReceiver.class.getSimpleName();
private static final String EXTRA_WAKE_LOCK_ID = "com.backendless.wakelockid";
private static final Map<Integer, PowerManager.WakeLock> activeWakeLocks = new HashMap<>();
public static final String EXTRA_MESSAGE_ID = "com.backendless.messageid";
private static int mNextId = 1;
public BackendlessBroadcastReceiver()
{
}
public Class<? extends BackendlessPushService> getServiceClass()
{
return BackendlessPushService.class;
}
/**
* @deprecated Extend {@link BackendlessPushService} instead.
*/
@Deprecated
public void onRegistered( Context context, String registrationId )
{
}
/**
* @deprecated Extend {@link BackendlessPushService} instead.
*/
@Deprecated
public void onUnregistered( Context context, Boolean unregistered )
{
}
/**
* @deprecated Extend {@link BackendlessPushService} instead.
*/
@Deprecated
public boolean onMessage( Context context, Intent intent )
{
return true;
}
/**
* @deprecated Extend {@link BackendlessPushService} instead.
*/
@Deprecated
public void onError( Context context, String message )
{
throw new RuntimeException( message );
}
@Override
public final void onReceive( Context context, Intent intent )
{
if( !Backendless.isInitialized() )
Backendless.initApplicationFromProperties( context );
BackendlessPushService.enqueueWork( context, getServiceClass(), intent );
}
}
BackendlessBroadcastReceiver calls onMessage. If the method returns false, then the default visualization logic is disabled. In your implementation of onMessage you can do whatever you want with the push notification.