distriqt / airnativeextensions

DEPRECATED: Original repository of the distriqt native extensions and is no longer maintained. Please see our site for the latest ANEs
https://airnativeextensions.com
2 stars 0 forks source link

PushNotification to multi users #259

Closed Alainzz closed 9 years ago

Alainzz commented 9 years ago

Hello,

About Android and IOS is it possible to send a notification by user for one application ? If yes how can I do please ?

Exemple: I have an application A installed in mobile user A1 and user A2 and user A3 I would like send a notification to only user A1 or only user A2 and A3 etc...

Thank you for your help

koriner commented 9 years ago

This kind of logic would need to be implemented on the server side, wherever you are controlling your push notifications from.

You can target users directly via their device tokens, so you can send a notification to whichever devices you want.

Alainzz commented 9 years ago

Hi

Sorry but it Is not a good answer for me HOW CAN I DO ?? PLEASE

Thank You your help Best regards Alain

-----Message d'origine----- De : "koriner" notifications@github.com Envoyé : ‎29/‎10/‎2014 10:25 À : "distriqt/airnativeextensions" airnativeextensions@noreply.github.com Cc : "Alainzz" alain2003fr@yahoo.fr Objet : Re: [airnativeextensions] PushNotification to multi users (#259)

This kind of logic would need to be implemented on the server side, wherever you are controlling your push notifications from. You can target users directly via their device tokens, so you can send a notification to whichever devices you want. — Reply to this email directly or view it on GitHub.=

minimedj commented 9 years ago

Hi Alainzz, it is possible. I do it perfectly with Distriqt ANE.

I assume you want to send PUSH notification, for that you need to do the followoing:

  1. You need to ask your users in your application to enable PUSH notification for it.
  2. When user agrees to enables PUSH notification you will get a PUSH-TOKEN for this device. You need this PUSH-TOKEN in order to send PUSH notifications to this particular user/device, so..
  3. You need to send PUSH-TOKEN to your server and save it in the user profile (you have to implement all this stuff on the server)
  4. When user A whats to send PUSH to user B you need to send request to the server to do so.
  5. On server you have to find PUSH-TOKEN for user B and send PUSH message via Apple Service.

This is just brief explanation so you get idea of how it works.

p.s. Remember that one user can have multiple devices so it could be more that 1 PUSH-TOKEN per user.

p.s.s. And don't forget to obtain proper certificates from you Apple Dev Account to send PUSH notifications.

Best regards, Max

marchbold commented 9 years ago

@Alainzz Max is exactly right, I just wanted to add a few things.

This is not something that can be done "safely" from the device but something that you should integrate into your application server.

The implementation will need to be split based on the destination user's device i.e. you'll need to use APNS to send notifications to iOS and GCM to Android, so you'll have to take into account whether the user is on Android or iOS or event whether your application allows interaction between the platforms.

Cheers, Michael

Alainzz commented 9 years ago

Hello minimedj,

Thank you a lot for your explanation. My problem is.

1) How can I take the user token 2) How send push with this toke

Do you have a little sans source please ? Exemple currenly for Android I send a Push Notification with this PHP script Where can I modify my script for add the device token of the user A to be sur to send it to user A


<?php

/**

// Message to send $message = "the test message"; $tickerText = "ticker text message"; $contentTitle = "content title"; $contentText = "content body";

// Put your device token here (without spaces): $registrationId = '???????????????????????????????????????????????????????????????????';

// GCM API Key $apiKey = "????????????????????????????????????????????????????????????????"; $sound='default';

$response = sendNotification( $apiKey, array($registrationId), array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, 'sound' => $sound, 'contentText' => $contentText) );

echo $response; echo "\ncomplete...\n";

//////////////////////////////////////////////////////////////////////////////// //
//

/**

// error_log(json_encode($data)); // error_log($response);

return $response;

}


Thank you a lot for your help

minimedj commented 9 years ago

@Alainzz I won't answer you about server side because your question is about php. I am not expert in php, I am expert in Erlang. Anyways it must be very simple curl request to PUSH server. I believe you will find a way to handle it. Just remember - you need to obtain proper certificates from iTunes (in case you use iOS, I have to experience with Android).

This is my helper class that does all stuff with PUSH (I removed code that not related to this subject but this is pretty much everything you need to do ti handle PUSH stuff):

package utils {
import com.distriqt.extension.pushnotifications.PushNotifications;
import com.distriqt.extension.pushnotifications.events.PushNotificationEvent;

import starling.events.EventDispatcher;

/**
 * Push Notification Manager
 */
public class PushNotificationManager extends EventDispatcher {

    /**
     * Shows system "Enable Push Notifications" popup.
     */
    public static function showEnablePushNotificationsPopup():void {
        try {
            if (PushNotifications.service.getDeviceToken() == "")
                PushNotifications.service.register("GCM_SENDER_ID"); // note <-- replace me with google gcm id
        } catch (e:Error) {
            trace(e.message + "\n" + e.getStackTrace());
        }
    }

    public static function init():void {
        try {
            PushNotifications.init(Settings.DISTRIQT);
            if (PushNotifications.isSupported) {
                PushNotifications.service.addEventListener(PushNotificationEvent.REGISTER_SUCCESS, registerSuccessHandler);
                PushNotifications.service.addEventListener(PushNotificationEvent.UNREGISTERED, unregisteredSuccessHandler);
                PushNotifications.service.addEventListener(PushNotificationEvent.NOTIFICATION, notificationHandler);
                PushNotifications.service.addEventListener(PushNotificationEvent.ERROR, errorHandler);
                PushNotifications.service.register("GCM_SENDER_ID");
            }
        } catch (e:Error) {
            trace(e.message + "\n" + e.getStackTrace());
        }
    }

    private static function registerSuccessHandler(event:PushNotificationEvent):void {
        trace("PN registration succeeded with reg ID: \n" + event.data);
        // save push_key on server
    }

    private static function unregisteredSuccessHandler(event:PushNotificationEvent):void {
        trace("PN un-registration succeeded");
        // remove push_key from server
    }

    private static function notificationHandler(event:PushNotificationEvent):void {
        trace("Remote notification received");
        try {
            // here you handle PUSH notification
            var obj:Object = JSON.parse(event.data);

        } catch (e:Error) {
            trace("ERROR:: " + e.message);
        }
    }

    private static function errorHandler(event:PushNotificationEvent):void {
        // do nothing
        trace("ERROR:: " + event.data);
    }
}
}

You have to call PushNotificationManager.init(); somewhere it the start of you app.

I just copied it from my sources. It work just fine for me.

Alainzz commented 9 years ago

Hello minimedj,

Thank for your source but I think it's the same to me your it's for IOS me for Android But when you send a push with your souce your send for ALL user right ? Me I would like to send a Push for ONE user exemple User A tomorrow to user B etc..

Could you explain me how can I do please ?

Really thank you a LOT for your help minimedj

Best regards

minimedj commented 9 years ago

@Alainzz it seems for me that do not understand basics of PUSH technology. The source I provided is ActionScript - so it runs on client, there is nothing about sending PUSH notifications. To send PUSH notification you need your server.

Please watch this video to get idea of how it works: https://www.youtube.com/watch?v=ATYhOlK11QM

Alainzz commented 9 years ago

Hello It's terrible for me you dont understant my request.My pushnotification WORK correctly I would like juste know if it's possible to send a push for a particular user do you understand me ? please.............................. Application     USER AP                        A AP                        B AP                        C I would like in my server send a push notification to A.  and other day I would like to send a push notification to B and other day I would like to send a push notification to A and B etc... I hope this times you understand me Thank you for your help Best regards

minimedj commented 9 years ago

@Alainzz I already told you how to do it.

Application   USER    PUSH-TOKEN
---------------------------------
AP            A       Token-A
AP            B       Token-B
AP            C       Token-C

To send PUSH notification to user A you use Token-A To send PUSH notification to user B you use Token-B etc...

Do you understand that for each user you have to save it's PUSH-TOKEN?

Alainzz commented 9 years ago

OK if I understand but again how can I do in my server to send only for an user A or B with token ? The token is special device right ?

sorry It's not clean

Thank you for you help

koriner commented 9 years ago

@Alainzz We can't really help you with your server side code since we don't know how it works.

Basically you would just need to query your database to find whichever users you want to send the notification to, and then select their device tokens - which you then use when you make the request to send push notifications from APNS or GCM.

Alainzz commented 9 years ago

I understand now ;) We can close the case Thank you very VERY mutch for your help

Best regards