Asked  8 Months ago    Answers:  5   Viewed   4 times

I have a widely distributed java swing application that is a web service client from a JAX WebService EJB on a Glassfish 3.1.2 server.

I want to be able to distribute a String notification to all users that stays active until they have read it. The notifications only need to exist within the Swing client.

I have created a superuser web portal to enter the String data and save it to a database.

My question is:

  1. What is the best technology to (push)distribute this data String Notification to my clients?
  2. How should I arcitect the database to know if the notification has been viewed? (So I can stop showing the "New Notification" on the client)

Or if there is a reference to a guide that would work wonderfully, I couldn't find one.

My Ideas:

  • Have the client call a webservice every 10 minutes to check if there are any new notifications
  • For the database create a notification table and notifications seen. Link my users table to notifications seen table. Notifications seen is very basic with just 3 columns: NotificationID, UserID, TimeSeen.

 Answers

2

One way of simulating push notifications is long polling. This technique is called Comet or Reverse AJAX. While it's more common in REST based services, it can be just as easily accomplished in JAX-WS.

For JAX-WS you will want to investigate:

  • asynchronous web service invocation
  • JAX-WS client APIs ... Using the JAX-WS asynchronous programming model

Have the client call a webservice every 10 minutes to check if there are any new notifications

Instead with long polling, you make the initial client connection right away. But instead of the server responding immediately, it hangs onto the connection (asynchronously). Then when a notification needs to be pushed, it responds back on the existing connection.

With this technique, as soon as information is available on the server, it will be "pushed" to the client.

For the database create a notification table and notifications seen. Link my users table to notifications seen table. Notifications seen is very basic with just 3 columns: NotificationID, UserID, TimeSeen.

Sounds good. Expose it as a JAX-WS service. When the client receives the message, have them invoke it with the NotificationID.

Something like:

NotificationService svc = ...;
UserId userId = ...;

AsyncHandler<Notification> handler = new AsyncHandler<Notification>()
{
    public void handleResponse (Response<Notification> response)
    {
       Notification notification = response.get();

       // update swing gui

       NotificationID notificationId = notifcation.getId();

       svc.markNotificationAsSeen(userId, notificationId);

       // continue polling forever (or put in some logic to stop)
       svc.getNotificationAsync(userId, this);
    }
};

Future<?> invocation = svc.getNotificationAsync(userId, handler);
Saturday, October 2, 2021
 
5

I recommend you either rename your variable from wsCtxt to wsContext or assign the name attribute to the @Resource annotation. The J2ee tutorial on @Resource indicates that the name of the variable is used as part of the lookup. I've encountered this same problem using resource injection in Glassfish injecting a different type of resource.

Though your correct name may not be wsContext. I'm following this java tip. If you like the variable name wsCtxt, then use the name attribute in the variable declaration:

@Resource(name="wsContext") WebServiceContext wsCtxt;

Thursday, July 22, 2021
 
Pegues
 
2

Probably the best but most complex is WS-Security with various authentication method. But it is most complex and its good for enterprise enviroment. It allows you to create end-to-end auth and there are lots of options. You can in simple case e.g. use Web Services Security UsernameToken Profile

    <S12:Envelope xmlns:S11="..." xmlns:wsse="..." xmlns:wsu= "...">
  <S12:Header>
  ...
    <wsse:Security>
      <wsse:UsernameToken>
        <wsse:Username>NNK</wsse:Username>
        <wsse:Password Type="...#PasswordDigest">weYI3nXd8LjMNVksCKFV8t3rgHh3Rw==</wsse:Password>
        <wsse:Nonce>WScqanjCEAC4mQoBE07sAQ==</wsse:Nonce>
        <wsu:Created>2003-07-16T01:24:32</wsu:Created>
      </wsse:UsernameToken>
    </wsse:Security>
  ...
  </S12:Header>
...
</S12:Envelope>

I don't know what library you use, but here is a nice article how to install Rampart into Axis2 and implement UsernameToken handling.

But in some, simplified cases you can simply make HTTP Basic Authentication to web server (through SSL). This may be worst solution but sometimes could be easiest to implement. Another solution, not connected with soap can be mutual authenticated SSL (with client auth).

Monday, August 2, 2021
 
sathiya
 
3

Try to send device ID of multiple devices as an array. In your case,

$registration_ids must be an array of device IDs.

E.g

$registration_ids = array('Device ID 1', 'Device ID 2');
Thursday, August 12, 2021
5

You can create your own php file.

Source https://github.com/hasyapanchasara/PushKit_SilentPushNotification

Use below structure to achieve your task.

Use this simplepush.php file

<?php

// Put your device token here (without spaces):


      $deviceToken = '1234567890123456789';
//


// Put your private key's passphrase here:
$passphrase = 'ProjectName';

// Put your alert message here:
$message = 'My first push notification!';



$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
//  'ssl://gateway.push.apple.com:2195', $err,
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body

$body['aps'] = array(
                     'content-available'=> 1,
                     'alert' => $message,
                     'sound' => 'default',
                     'badge' => 0,
                     );



// Encode the payload as JSON

$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

Use below commands to create pem file and use it in above code

$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem

# Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert.  
$ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:

Verifying - Enter PEM pass phrase:

# To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings.
$ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem

Enter pass phrase for PushChatKey1.pem:

writing RSA key

# To join the two .pem file into one file:
$ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem

After that go to simplepush.php location and fire command -> php simplepush.php

This way you can test your push kit notification setup architecture.

https://zeropush.com/guide/guide-to-pushkit-and-voip

https://www.raywenderlich.com/123862/push-notifications-tutorial

Download

Updated with Swift 4 code

import UIKit
import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate {

var window: UIWindow?

var isUserHasLoggedInWithApp: Bool = true
var checkForIncomingCall: Bool = true
var userIsHolding: Bool = true

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    if #available(iOS 8.0, *){


        let viewAccept = UIMutableUserNotificationAction()
        viewAccept.identifier = "VIEW_ACCEPT"
        viewAccept.title = "Accept"
        viewAccept.activationMode = .Foreground
        viewAccept.destructive = false
        viewAccept.authenticationRequired =  false

        let viewDecline = UIMutableUserNotificationAction()
        viewDecline.identifier = "VIEW_DECLINE"
        viewDecline.title = "Decline"
        viewDecline.activationMode = .Background
        viewDecline.destructive = true
        viewDecline.authenticationRequired = false

        let INCOMINGCALL_CATEGORY = UIMutableUserNotificationCategory()
        INCOMINGCALL_CATEGORY.identifier = "INCOMINGCALL_CATEGORY"
        INCOMINGCALL_CATEGORY.setActions([viewAccept,viewDecline], forContext: .Default)

        if application.respondsToSelector("isRegisteredForRemoteNotifications")
        {
            let categories = NSSet(array: [INCOMINGCALL_CATEGORY])
            let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])

            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as? Set<UIUserNotificationCategory>)

            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        }

    }
    else{
        let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
        application.registerForRemoteNotificationTypes(types)
    }


    self.PushKitRegistration()

return true
}
//MARK: - PushKitRegistration

func PushKitRegistration()
{

    let mainQueue = dispatch_get_main_queue()
    // Create a push registry object
    if #available(iOS 8.0, *) {

    let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)

    // Set the registry's delegate to self

    voipRegistry.delegate = self

    // Set the push type to VoIP

    voipRegistry.desiredPushTypes = [PKPushTypeVoIP]

    } else {
    // Fallback on earlier versions
    }


}


@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
    // Register VoIP push token (a property of PKPushCredentials) with server

    let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
    count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")

    print(hexString)


}


@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {

    // Process the received push

    // Below process is specific to schedule local notification once pushkit payload received

    var arrTemp = [NSObject : AnyObject]()
    arrTemp = payload.dictionaryPayload

    let dict : Dictionary <String, AnyObject> = arrTemp["aps"] as! Dictionary<String, AnyObject>


    if isUserHasLoggedInWithApp // Check this flag then only proceed
    {

        if UIApplication.sharedApplication().applicationState == UIApplicationState.Background || UIApplication.sharedApplication().applicationState == UIApplicationState.Inactive
        {

            if checkForIncomingCall // Check this flag to know incoming call or something else
            {

                var strTitle : String = dict["alertTitle"] as? String ?? ""
                let strBody : String = dict["alertBody"] as? String ?? ""
                strTitle = strTitle + "n" + strBody

                let notificationIncomingCall = UILocalNotification()

                notificationIncomingCall.fireDate = NSDate(timeIntervalSinceNow: 1)
                notificationIncomingCall.alertBody =  strTitle
                notificationIncomingCall.alertAction = "Open"
                notificationIncomingCall.soundName = "SoundFile.mp3"
                notificationIncomingCall.category = dict["category"] as? String ?? ""

                //"As per payload you receive"
                notificationIncomingCall.userInfo = ["key1": "Value1"  ,"key2": "Value2" ]


                UIApplication.sharedApplication().scheduleLocalNotification(notificationIncomingCall)

            }
            else
            {
                //  something else
            }

        }
    }


}

//MARK: - Local Notification Methods

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification){

    // Your interactive local notification events will be called at this place

}


}

Let me know if i could help you somewhere.

Have a happy coding.

Wednesday, September 15, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :